JavaScript Interview Question (Confusing but Important 🤯) What will be the output? console.log([] == []); console.log([] === []); console.log({} == {}); console.log({} === {}); 👉 Answer: All will be false Why? Arrays and objects in JavaScript are stored in memory by reference, not by value. Each [] or {} creates a new object with a different memory address, and JavaScript compares references — not structure or content. Same shape ≠ same reference 📌 This is one of the most common JavaScript interview traps. #JavaScript #InterviewQuestions #WebDevelopment #Frontend #Backend #CodingTips
JavaScript Interview Question: Arrays and Objects by Reference
More Relevant Posts
-
🧠 JavaScript Interview Trap – Do You Know the Output? Consider this: console.log([] == []); console.log([] === []); console.log({} == {}); console.log({} === {}); 👉 Output: All four will return false 💡 Why? In JavaScript, arrays and objects are reference types, not primitive values. Every time you create "[]" or "{}", JavaScript allocates a new memory reference. So when you compare them: - "[] === []" → different references → false - "{}" === "{}" → different references → false Even if they look identical, JavaScript compares references, not structure or content. ⚡ Key Takeaway: Same shape ≠ Same reference This is one of the most common JavaScript interview traps for frontend developers. #JavaScript #FrontendDevelopment #WebDevelopment #MERNStack #CodingInterview #JSConcepts
To view or add a comment, sign in
-
💻 JavaScript Interview Question You will be given a string and two indexes (a and b). Your task is to reverse the portion of that string between those two indices inclusive. str = "javascript", a = 1, b = 5 --> "jcsavaript" East right ? But rather than traversing the string and using 2-pointer technique, you have to use the inbuilt functions of javascript. This tests your understanding of the language and how it works internally. function solve(st, a, b){ return st.slice(0,a)+(st.slice(a, b+1).split("").reverse().join(""))+st.slice(b+1); } Feel free to discuss more in comments 🖐 #javaScript #CodingInterview #WebDevelopment #Frontend #React
To view or add a comment, sign in
-
🚨 JavaScript Interview Question What will be the output? const obj = { name: "Frontend", regularFunc: function () { console.log(this.name); }, arrowFunc: () => { console.log(this.name); } }; obj.regularFunc(); obj.arrowFunc(); Both functions are inside the same object. But the output is different. This question tests understanding of: • this binding • Arrow functions vs regular functions • Execution context in JavaScript Many developers assume both will print the same value. What do you think the output will be? #JavaScript #FrontendInterview #ReactJS #FrontendDeveloper #JavaScriptConcepts #ProductBasedCompany
To view or add a comment, sign in
-
Javascript Event Loop - One of the Most Asked Interview Questions If you’ve ever prepared for a frontend interview, you’ve definitely come across this question: 👉 “How does the JavaScript Event Loop work?” Understanding the Event Loop is crucial because it explains how JavaScript handles asynchronous operations despite being single-threaded. 💡 In simple terms: JavaScript executes code using a call stack. Async tasks (like setTimeout, Promises, API calls) are handled by Web APIs Once completed, they move to callback queues. The Event Loop continuously checks and pushes tasks back to the call stack when it's empty. ⚡ Key concepts every developer should know: Call Stack Callback Queue Microtask Queue (Promises > setTimeout priority) Execution Order 🎯 Mastering this concept not only helps in interviews but also improves your ability to write efficient, non-blocking code. I’ve created a simple explanation (with examples) to make this concept easy to understand 👇 #JavaScript #Frontend #WebDevelopment #EventLoop #InterviewPrep #AsyncProgramming
To view or add a comment, sign in
-
One of the most common JavaScript interview questions: "Why does setTimeout with 0ms delay not run immediately?" Most developers cannot answer this correctly. Here is the full explanation: JavaScript is single-threaded. It can only do one thing at a time. The Event Loop is how it manages everything else. The execution order is always the same: 1 — Synchronous code runs first All regular code on the Call Stack executes immediately. 2 — Microtasks run second Promises, async/await — these run before anything else once the Call Stack is empty. 3 — Macrotasks run last setTimeout, setInterval, DOM events — these wait until ALL microtasks are done. This is why setTimeout with 0ms still runs after a Promise. The Promise is a microtask. setTimeout is a macrotask. Microtasks always win. Understanding this prevents real bugs in production — async state updates, race conditions, unexpected render order. Save this post for your next async debugging session. Have you ever been confused by JavaScript async order? Drop a comment below. #JavaScript #WebDevelopment #Frontend #SoftwareEngineering #AsyncJavaScript
To view or add a comment, sign in
-
-
🚨JavaScript Interview Question What will be the output? function greet(name) { if (name === undefined) { console.log("Hello, guest!"); } else { console.log("Hello, "+ name); } greet(); greet("Anas"); greet("Anas", "How are you?"); Looks simple... but there's a twist What will be the output? Why does the last call behave differently? Bonus: How does JavaScript handle extra arguments? #JavaScript #FrontendInterview #WebDevelopment #CodingInterview #ProductBasedCompany
To view or add a comment, sign in
-
🚨 JavaScript Interview Question What will be the output? const obj = { name: "Frontend", greet: function () { console.log(this.name); } }; const greet = obj.greet; greet(); greet.call(obj); greet.bind(obj)(); This question tests understanding of: • this binding • call() • bind() • Function invocation context Many developers expect all three calls to behave the same. But JavaScript handles this differently depending on how the function is invoked. What do you think the output will be? #JavaScript #FrontendInterview #ReactJS #FrontendDeveloper #JavaScriptConcepts #ProductBasedCompany
To view or add a comment, sign in
-
🚀 **JavaScript Interview Question** What will be the output of this code? ```js const obj = { a: { b: 0 } }; const v1 = obj?.a?.b || 42; const v2 = obj?.a?.b ?? 42; console.log(v1, v2); ``` 🤔 **Think before you scroll...** --- #JavaScript #Frontend #WebDevelopment #CodingInterview #ReactJS
To view or add a comment, sign in
-
🚨 JavaScript Async Interview Question What will be the output? async function test() { console.log("1"); setTimeout(() => { console.log("2"); }, 0); await Promise.resolve(); console.log("3"); } console.log("4"); test(); console.log("5"); This question tests understanding of: • Call Stack • Async/Await behavior • Microtask queue (Promises) • Macrotask queue (setTimeout) • JavaScript Event Loop Many developers get the order wrong the first time. What do you think the output will be? #JavaScript #FrontendInterview #AsyncJavaScript #EventLoop #ReactJS #FrontendDeveloper #ProductBasedCompany
To view or add a comment, sign in
-
🚀 𝗖𝗹𝗼𝘀𝘂𝗿𝗲𝘀 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 — 𝗢𝗻𝗲 𝗼𝗳 𝘁𝗵𝗲 𝗠𝗼𝘀𝘁 𝗔𝘀𝗸𝗲𝗱 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀! If you’ve ever prepared for a JavaScript interview, you’ve definitely come across closures. But do you truly understand them? In simple terms: A closure is when a function “remembers” the variables from its outer scope even after that outer function has finished executing. 𝗪𝗵𝘆 𝗶𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄𝗲𝗿𝘀 𝗹𝗼𝘃𝗲 𝘁𝗵𝗶𝘀 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻? Because it tests your understanding of: • Scope • Lexical environment • Memory behavior in JavaScript Quick Example: function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const counter = outer(); counter(); // 1 counter(); // 2 Here, inner() still has access to count even after outer() is executed — that’s a closure! 🔥 𝗥𝗲𝗮𝗹-𝗪𝗼𝗿𝗹𝗱 𝗨𝘀𝗲 𝗖𝗮𝘀𝗲𝘀 • Data privacy (encapsulation) • Creating counters & stateful functions • Event handlers & callbacks • Memoization & performance optimizations 💬 If you’re preparing for interviews, don’t just memorize definitions — understand how closures work under the hood. #frontend #javascript #reactjs #nextjs #community
To view or add a comment, sign in
More from this author
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