JavaScript has a lot of tricky questions, and one of my favorites is this one 👇 console.log([] == ![]) Most developers expect this to be false. But the actual output is: true Why? Step 1: ![] becomes false Because in JavaScript, an empty array is truthy. Step 2: Now the comparison becomes [] == false Step 3: JavaScript converts both values during loose equality comparison false becomes 0 [] becomes "" Then: "" becomes 0 Final comparison: 0 == 0 That’s why the result is true. This is a perfect example of why == can create unexpected results. That’s also why many developers prefer using === for safer and more predictable comparisons. JavaScript is powerful, but type coercion can be surprisingly tricky. Have you seen a stranger JS behavior than this one? 😄 #JavaScript #FrontendDevelopment #WebDevelopment #Programming #SoftwareEngineering #JSInterview #ReactJS
Why JavaScript's [] == ![] is true
More Relevant Posts
-
JS Pop Quiz: Did we just overwrite the Admin?! Let’s see who really understands JavaScript memory allocation! 👨💻👩💻 Look at the code snippet from @codewithsarir. We have a user1 object. We assign it to user2, and then change user2's role to 'Guest'. Question: What does console.log(user1.role) actually print? A) 'Admin' (Because we only changed user2) B) 'Guest' (Because they share the same reference) C) undefined D) It throws a TypeError Hint: Think about how JavaScript handles Objects versus Primitive types like strings. Does = make a copy, or just point to the same address? 🤔 Drop your guess in the comments before you test it in your IDE! 👇 Hashtags: #JavaScript #CodingQuiz #WebDesign #ProgrammerLife #Developers #LearnToCode #JS #Frontend #creators #codinglife #programmer
To view or add a comment, sign in
-
-
💡 JavaScript Basics That Still Confuse Many Developers… Let’s break down a classic: Function Declaration vs Function Expression 👇 🔹 Function Declaration function greet() { console.log("Hello!"); } ✔ Hoisted (you can call it before it’s defined) ✔ Cleaner and easier to read 🔹 Function Expression const greet = function() { console.log("Hello!"); }; ✔ Not hoisted (must be defined before use) ✔ More flexible (can be anonymous, used in callbacks, etc.) 🚀 Key Difference: Function declarations are available throughout the scope, while function expressions behave like variables. 📌 Pro Tip: Prefer function expressions (especially arrow functions) in modern JavaScript for better control and predictability. #JavaScript #WebDevelopment #CodingBasics #Frontend #LearnToCode
To view or add a comment, sign in
-
-
JavaScript is simple… until it isn’t 😅 Here are a few things that confused me (and probably you too): 👉 [] + [] = "" 👉 [] + {} = "[object Object]" 👉 {} + [] = 0 And the classic: 👉 typeof null === "object" 🤯 JavaScript isn’t weird… it’s just misunderstood. Once you understand type coercion, execution context, and closures, everything starts making sense. 💡 My advice: Don’t just “use” JavaScript — understand how it works internally. What’s the most confusing JS behavior you’ve faced? 👇 #javascript #webdevelopment #frontend #programming
To view or add a comment, sign in
-
JavaScript Loops: When to use what? 🔁 Loops let you run code multiple times without repeating yourself. Two basics you must know: 🔹 for loop – when you know the number of iterations ```js for (let i = 0; i < 5; i++) { console.log(i); // 0 1 2 3 4 } ``` 🔹 while loop – when you don't know the exact count, only the condition ```js let i = 0; while (i < 5) { console.log(i); i++; } ``` ✅ Key difference: · for = known iterations (e.g., loop through array length) · while = condition-based (e.g., keep prompting until valid input) Master loops, and you master control flow 💪 Which loop do you use more often? Drop a comment 👇 #JavaScript #CodingBasics #WebDevelopment #JS #ProgrammingTips #LearnToCode
To view or add a comment, sign in
-
-
🚀 JavaScript Array Methods Cheat Sheet 🚀 Level up your JS game (especially in React!) with these essentials. Simple explanations + examples: • filter() 🔍: New array of matching items. numbers.filter(n => n > 5) → • map() ✨: Transform every item. names.map(name => name.toUpperCase()) → ["ALICE", "BOB"] • find() 🕵️: First match only. users.find(u => u.id === 1) • findIndex() 📍: Index of first match. • every() ✅: All true? scores.every(s => s > 60) → true/false • some() ➕: At least one true? • fill() 🧱: Fill with a value (mutates!). • concat() 🔗: Merge arrays safely. Pro Tip: Chain filter + map in React for clean lists! data.filter(item => item.active).map(item => <Item key={item.id} />) Master these = faster code + happier teams. 💪 #JavaScript #React #WebDev #CodingTips #Frontend #ArrayMethods #Developers #Programming #ReactJS #30DaysOfCode
To view or add a comment, sign in
-
-
setTimeout does nothing inside the JavaScript engine. It's a label. A facade. When you call it, JS hands the work off to a browser feature - the actual timer lives outside JavaScript entirely. The browser runs it independently while JS continues on to the next line. All the features we think of as "JavaScript" - timers, network requests, DOM interactions - are actually browser APIs. JS just has labels that trigger them. This is how JS avoids blocking. It doesn't wait. It delegates. The result comes back later, through a controlled channel called the callback queue. Next: the event loop - the single mechanism that controls when deferred code is allowed back into JavaScript. #JavaScript #WebDevelopment #Programming #SoftwareEngineering
To view or add a comment, sign in
-
JavaScript vs. TypeScript: The 'Cocomelon' Edition! Ever feel like your JavaScript code is a bit... chaotic? Like a toddler running around with no shoes? That’s where TypeScript comes in! Think of JavaScript as the fun, flexible playground where you can build anything quickly. It’s dynamic, it’s fast, but sometimes things get messy. TypeScript is like adding a 'Safety Helmet' and 'Rules' to that playground. It’s a superset of JavaScript that adds Static Typing. Why make the switch? Catch Bugs Early: Find errors while you type, not when the app crashes. Better Tooling: Enjoy super-powered autocompletion in VS Code. Scalability: It makes large projects much easier for teams to manage. Is your team Team JS or Team TS? Let's discuss below! #JavaScript #TypeScript #WebDevelopment #CodingLife #SoftwareEngineering #TechSimplified #Frontend #Programming
To view or add a comment, sign in
-
-
🚀 Day 979 of #1000DaysOfCode ✨ 4 Useful Number Functions in JavaScript (With Cool Examples) JavaScript provides many built-in number utilities — but most developers only use a few of them. In today’s post, I’ve shared 4 super useful number functions in JavaScript along with some cool and practical examples for each. These functions can help you handle number validation, formatting, and edge cases more effectively in real-world applications. Small utilities like these might look simple, but they can save you time and help you write cleaner and more reliable logic. Once you start using them properly, you’ll notice how often they come in handy while working with data. If you work with numbers, calculations, or user inputs in JavaScript, these functions are definitely worth knowing. 👇 Which JavaScript number function do you use the most in your projects? #Day979 #learningoftheday #1000daysofcodingchallenge #FrontendDevelopment #WebDevelopment #JavaScript #React #CodingCommunity #JSDevelopers
To view or add a comment, sign in
-
Most JS developers use closures daily without knowing it. counter() finished running, but increment still remembers count. That's a closure. How does it remember? When a function is created in JavaScript, it doesn't just save the code — it also saves a reference to the variables around it at that moment. So even after the outer function is gone, that reference stays alive in memory as long as the inner function exists. Think of it like this the inner function carries a backpack of its outer variables wherever it goes. 🎒 You already use this in React's useState, debounce, and event handlers. Once I understood this, my React bugs started making sense. 🙂 Did closures confuse you at first? Drop a comment 👇 #JavaScript #MERN #WebDevelopment #LearningInPublic
To view or add a comment, sign in
-
-
most developers don't understand closures and it breaks their loops. classic problem: everyone uses let now so this fixes itself but the problem is still there, just hidden. here's why it happens: - when you use var > it's function-scoped not block-scoped. - by the time the timeout runs, the loop is done and i is 3. - all three callbacks reference the fix with let: let is block-scoped so each iteration gets its own i . but here's what you should actually know: - this isn't a JavaScript problem. this is a closure problem. - every function closes over the variables around it. - understanding that changes everything about how you debug async code, event listeners, callbacks. stop memorizing the fix. understand why it happens. #javascript #typescript #webdevelopment #buildinpublic #reactjs
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