Yesterday poll answer and explanation console.log([2] == [2]); // false 👀 In JavaScript arrays are objects (reference types). So, when both sides are objects (reference types), JavaScript does not compare their values. Instead, it compares their memory references. console.log([2] == 2); // true 🤯 So here, left operand is an object (reference type) and right operand is a (primitive type) is number. In this case, type coercion happens. The reference type is first converted into a primitive. [2].toString() // "2" and it becomes "2" == 2 With ==, JavaScript converts string to number: Number("2") == 2, It becomes 2 == 2 So the final output is, false, true Hope this explanation is helpful to someone 😊 #JavaScript #FrontendDeveloper #WebDevelopment #LearningJavaScript #InterviewPrep
JavaScript Array Comparison Explained
More Relevant Posts
-
📚 Today I Learned: Scope Chain in JavaScript The scope chain in JavaScript is used to resolve variable values. When a variable is used, JavaScript looks for it in a specific order. 🔹 How it works: 1️⃣ JavaScript first checks the current scope. 2️⃣ If the variable is not found, it checks the outer (parent) scope. 3️⃣ This process continues until it reaches the global scope. 💻 Example: let a = 10; function outer() { let b = 20; function inner() { let c = 30; console.log(a, b, c); } inner(); } outer(); ✅ The inner() function can access c, b, and a because of the scope chain. #javascript #webdevelopment #frontenddeveloper #learninginpublic
To view or add a comment, sign in
-
JavaScript Tip 💡: Use the Array "at()" method to access last Array element easily! The "at()" method in JavaScript provides a simpler way to access elements in an array, especially the last one. Traditionally, getting the last element required using arr[arr.length - 1], but .at(-1) now handles this directly and more cleanly. With "at()", positive indices retrieve elements from the start, while negative indices count backward from the end. This makes .at(-1) a straightforward and readable alternative for accessing the last item in an array. Hope this helps ✅️ Do Like 👍 & Repost 🔄 #html #css #javascript #typescript #react
To view or add a comment, sign in
-
⚡ 1-Minute JavaScript Quickly check if all values in an array are truthy using a clean one-liner. Utility function :- const allTruthy = (arr) => arr.every(Boolean); //Usage :- allTruthy([1, "hello", true]); // true allTruthy([1, "", true]); // false 💡 Why this works: Array.every() checks if every element passes a condition. Passing Boolean converts each value to true or false. So it effectively checks if every value is truthy. 🔎 Values considered falsy in JavaScript: • false • 0 • "" (empty string) • null • undefined • NaN ⚙️ Practical use cases: ✔ Form validation ✔ API response checks ✔ Ensuring required values exist before processing Clean. Expressive. Very JavaScript. #JavaScript #FrontendDevelopment #CleanCode #WebDevelopment #DevTips
To view or add a comment, sign in
-
-
Javascript: Undefined vs null Ever seen undefined and null in JavaScript and felt confused? 🤔 You’re not alone. Many beginners mix them up. But the difference is actually very simple. Here’s the easy way to understand it: • undefined → A variable is declared but no value is assigned yet let name; console.log(name); // undefined • null → A developer intentionally sets an empty value let user = null; • undefined is automatic – JavaScript gives it by default. • null is intentional – The developer sets it manually. • Both mean “no value”, but the reason is different. Simple rule to remember: 👉 undefined = not assigned yet 👉 null = intentionally empty Understanding this small concept can help you avoid many bugs in JavaScript. #JavaScript #WebDevelopment #FrontendDevelopment #ProgrammingTips #LearnJavaScript #CodingForBeginners #SoftwareEngineering #TechEducation #JavaScriptDeveloper #DevCommunity
To view or add a comment, sign in
-
-
The JavaScript "this" Trap 🪤🔥 The Puzzle: What is the output? 🤔 const obj = { name: "JS", getName() { console.log(this.name); } }; const fn = obj.getName; fn(); Output: undefined Why? 🧠 In JavaScript, this depends on HOW a function is called, not where it is written. Lost Context: const fn = obj.getName only copies the function reference. Standalone Call: When you call fn(), there is no object (no dot) before the function. Global Context: It now runs in the Global Context (window object). Since window.name is not "JS", it returns undefined. How to Fix? 🛠️ ✅ Use .bind(): const fn = obj.getName.bind(obj); ✅ Use .call(): fn.call(obj); ✅ Use Arrow Functions: They inherit this from the surrounding scope. Interview Tip: 💡 Always check the "Call Site." No dot before the function call (like fn()) usually means this is lost! #JavaScript #CodingTips #365DaysOfCode #InterviewPrep #WebDev #FullStack #mern #react #node
To view or add a comment, sign in
-
🔥 Understanding this keyword in JavaScript The value of this depends on how a function is called, not where it is written. >> In the global scope console.log(this); this → refers to the global object (window in browsers) >> Inside an object method const user = { name: "Javascript", greet() { console.log(this.name); } }; user.greet(); // Javascript this → refers to the object calling the method ✔️ user.greet() → this is user >> In a regular function function show() { console.log(this);} show(); this → undefined (in strict mode) or → global object (non-strict mode) >> In arrow functions const obj = { name: "Javascript", greet: () => { console.log(this.name);}}; obj.greet(); // undefined this → does NOT have its own value It borrows from the surrounding scope 👉 That’s why arrow functions can surprise you! >> In event handlers button.addEventListener("click", function() { console.log(this);}); this → refers to the element that triggered the event #JavaScript #FrontendDevelopment #WebDevelopment #Coding #LearnToCode
To view or add a comment, sign in
-
Because events in JavaScript ""bubble"" up the DOM tree, you can attach a single event listener to the parent container and use event.target to figure out which child was clicked. JavaScript // Attach ONE listener to the parent <ul> document.getElementById('item-list').addEventListener('click', (event) => { // Check if the clicked element was a button if (event.target.tagName === 'BUTTON') { const itemId = event.target.dataset.id; console.log(`Item ${itemId} clicked!`); } }); This uses drastically less memory and automatically handles new items added to the list dynamically—no need to attach new listeners when the DOM updates! #JavaScript #WebPerformance #CodingInterviews #FrontendDev #TechTips"
To view or add a comment, sign in
-
-
From basic tags to advanced frameworks, here is the breakdown: ✅ Days 1-20: The Foundations (HTML/CSS) ✅ Days 20-40: The Engine (JavaScript/React) ✅ Days 40-70: Real-world Application & Design ✅ Days 80-100: Optimization & Polish Which stage do you find the most challenging? For me, it was definitely mastering Advanced JS! #TechCommunity #CodeNewbie #FrontendDeveloper #Roadmap
To view or add a comment, sign in
-
-
💡 JavaScript Trick Question: 3 + 2 + "7" In JavaScript, the answer is: 👉 "57" 🔍 Why? 🔹 JavaScript follows left-to-right evaluation and uses type coercion. ⚡ Key Insight : 🔹Once a string enters the expression, everything after that becomes a string operation. "In JavaScript, the moment a string joins the party, numbers stop adding and start concatenating." #JavaScript #WebDevelopment #CodingInterview #Frontend #JSConcepts
To view or add a comment, sign in
-
-
In JavaScript, the comparison operators == and === serve different purposes when evaluating two values. The == operator checks only the value, allowing for automatic type conversion before comparison. For instance, 5 == "5" evaluates to true because JavaScript converts the string "5" into the number 5. On the other hand, the === operator checks both the value and the data type without performing any conversion. Therefore, 5 === "5" results in false, as one is a number and the other is a string. This distinction highlights that === is stricter and safer than ==, leading many developers to prefer using === in modern JavaScript. Utilizing === helps to avoid unexpected results in your code.
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development