JavaScript Hacks - 😶🌫️ console.log(typeof null) still breaks smart developers in interviews. The output is "object". This confuses people because null is not an object. → JavaScript stores type info using internal tags, and null was given the same tag as objects in the first version of the language. → That decision shipped in 1995, and changing it now would break millions of websites. → The ECMAScript spec keeps this behavior for backward compatibility, even though it is wrong. → null is a primitive value that represents intentional absence, not a data structure. #javascript #frontend #hacks #interviewpreparation
JavaScript Hacks: Why console.log(typeof null) returns 'object'
More Relevant Posts
-
🚀 5 Tricky JavaScript Output Questions (Closures & Scope) Let’s test your JS fundamentals 👇 Try to guess the output before checking answers. 1️⃣ for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1000); } 2️⃣ let a = 10; (function () { console.log(a); let a = 20; })(); 3️⃣ function foo() { console.log(x); var x = 10; } foo(); 4️⃣ const obj = { a: 10, getA() { return this.a; } }; const fn = obj.getA; console.log(fn()); 5️⃣ console.log(typeof typeof 1); Answers below 👇 1️⃣ 3 3 3 (var is function-scoped, same reference) 2️⃣ ReferenceError (Temporal Dead Zone for `let`) 3️⃣ undefined (var hoisting without initialization) 4️⃣ undefined (`this` is lost when function is detached) 5️⃣ "string" (typeof 1 → "number", typeof "number" → "string") If you can explain *why*, you’re interview-ready 💯 #javascript #frontend #interviewprep #webdevelopment
To view or add a comment, sign in
-
🚨 One of the most confusing but important JavaScript concepts — Closure In JavaScript, a function can remember variables from its outer scope, even after the outer function has finished executing. How this works 👇 • outer() returns inner() • inner() still has access to count • Every call updates the same count value • Even after outer() finishes, count stays alive This behavior is called a Closure 📘 Closures are widely used in: • Counters • Data privacy • Event handlers • React hooks Understanding closures helped me understand how JavaScript actually works under the hood ⚡ #JavaScript #Closure #JSInterview #FrontendDevelopment #WebDevJourney #LearnInPublic
To view or add a comment, sign in
-
-
JavaScript Hacks - 😶🌫️ These two lines expose who actually understands JavaScript. ------------------ console.log([] == []); console.log({} == {}); ------------------ The output is false and false. This surprises people who think JavaScript compares values. → Arrays and objects live in memory, and JavaScript compares their memory addresses, not their contents. → Every new array or object gets a brand new reference, even if it looks identical. → [] and [] are two different memory locations, so the comparison fails immediately. → The same rule applies to objects, functions, and class instances. → JavaScript compares objects by reference, not by value. #javascript #frontend #hacks #interviewpreparation
To view or add a comment, sign in
-
-
🔥 JavaScript can “teleport” your variables. This is a classic interview trap — and it still catches experienced devs 👇 ❓What prints for console.log(a)? A) 10 B) undefined C) ReferenceError D) null ❓What happens at console.log(b)? A) 20 B) undefined C) ReferenceError D) null 💬 Drop your answers + reasoning below 👇 #CodeSnatch #javascript #webdevelopment #frontend #interviewprep #codinginterviews #developers
To view or add a comment, sign in
-
-
🔹 Aggregate Error in JavaScript Promises AggregateError is a special error type used when multiple errors need to be wrapped into a single error object. You most commonly see it with Promise.any(): if all promises reject, Promise.any() rejects once with an AggregateError that contains all rejection reasons in error.errors. 👉 Quick example with Promise.any() js const p1 = Promise.reject(new Error("API 1 failed")); const p2 = Promise.reject(new Error("API 2 failed")); const p3 = Promise.reject("Network timeout"); Promise.any([p1, p2, p3]) .then((result) => { console.log("First success:", result); }) .catch((error) => { console.log(error instanceof AggregateError); // true console.log(error.name); // "AggregateError" console.log(error.message); // e.g. "All Promises rejected" console.log(error.errors); // [Error("API 1 failed"), Error("API 2 failed"), "Network timeout"] }); 🧠 Why it matters? Helps handle many async failures in one catch block while still keeping each individual error inside error.errors. Very useful in parallel operations like multiple API calls or validations where all attempts might fail. #JavaScript #ES2021 #Promise #AggregateError #AsyncJS #WebDevelopment
To view or add a comment, sign in
-
🤔 Quick JavaScript question What do you think this logs? console.log(typeof null); console.log(typeof undefined); 👉 Output "object" "undefined" Surprising, right? 😄 🔹 null is meant to represent an intentional absence of value, but due to a legacy JavaScript bug, typeof null returns "object". 🔹 undefined means a variable exists but has not been assigned a value, and typeof undefined correctly reflects that. #JavaScript #WebDevelopment #NodeJS #MERN #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 7 – Daily Tech Dose (JavaScript | Mid-Level) 💡 Today’s Topic: Closures in JavaScript (🔥 One of the MOST asked interview concepts) 🤔 Predict the Output function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const fn1 = outer(); fn1(); fn1(); ✅ Output 1 2 ⸻ 🧠 What’s Really Happening? • inner() remembers the variable count even after outer() finishes. • This memory + function combo is called a Closure. • Each time fn1() is called, it updates the same count variable. 👉 Closures allow: • Data privacy 🔒 • State management without global variables • Powerful patterns in React, callbacks & async JS ⸻ 🔥 Interview Twist What will this print? const fn2 = outer(); fn2(); ➡️ Answer: 1 (Each call to outer() creates a new closure 👀) 💬 Quick Poll Where have you seen closures used most? • React Hooks 🪝 • Event Handlers • setTimeout / setInterval • Still confused 😅 ⸻ 🔖 Hashtags #JavaScript #Closures #FrontendDevelopment #WebDev #CodingInterview #LearnJavaScript #100DaysOfCode #TechDaily #VivekVishwakarma Want Day 8 (Async JS / Promises) next or DSA with JS? 😎
To view or add a comment, sign in
-
-
Most JavaScript devs pause on this 👀 Even with async/await experience. No frameworks. No libraries. Just JavaScript fundamentals. Question 👇 async function test() { try { return Promise.reject("Error"); } catch (e) { console.log("caught"); } } test().catch(console.log); ❓ What will be printed to the console? A. caught B. Error C. Nothing D. Both caught and Error Why this matters Many developers assume: try/catch handles all errors inside async functions Promise.reject() behaves like throw That assumption is wrong. When fundamentals aren’t clear: error handling feels unpredictable bugs slip through silently debugging turns into guesswork Strong developers don’t guess. They understand how async functions actually propagate errors. 👇 Drop your answer in the comments Did this one make you think twice? #JavaScript #JSFundamentals #AsyncAwait #Promises #WebDevelopment #FrontendDeveloper #FullStackDeveloper #CodingInterview #DevelopersOfLinkedIn #DevCommunity #VibeCode
To view or add a comment, sign in
-
-
One of the most common bugs in JavaScript and React comes from copying objects the wrong way 😅 Shallow Copy vs Deep Copy In JavaScript, when you copy an object using the spread operator (...), you are only creating a shallow copy. Example: ``` const user = { name: “Ali”, skills: [“JS”, “React”] }; const copy = { …user }; copy.skills.push(“Node”); console.log(user.skills); // [“JS”, “React”, “React”, “Node”] ``` Why did the original object change? Because: • name was copied • but skills is still pointing to the same array in memory Both user.skills and copy.skills reference the same place. To create a real independent copy, use: const deepCopy = structuredClone(user); Now changing deepCopy will not affect user. 📌 In React, this mistake can cause: • state bugs • missing re-renders • very confusing UI issues Understanding how memory and references work is a game changer. #JavaScript #React #Frontend #WebDevelopment #Programming
To view or add a comment, sign in
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