JavaScript Interview Questions and Answers

🚫 𝗧𝗵𝗶𝘀 𝗶𝘀 𝘄𝗵𝗲𝗿𝗲 𝗺𝗼𝘀𝘁 𝗱𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿𝘀 𝗙𝗔𝗜𝗟 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗶𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄𝘀. Not because the questions are hard… But because the concepts are deceptively simple. Let’s test your depth 👇 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝟭: 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻 + 𝗛𝗼𝗶𝘀𝘁𝗶𝗻𝗴 𝗧𝗿𝗮𝗽 𝑣𝑎𝑟 𝑥 = 1; 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑡𝑒𝑠𝑡() {   𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔(𝑥);   𝑣𝑎𝑟 𝑥 = 2; } 𝑡𝑒𝑠𝑡(); 𝗢𝘂𝘁𝗽𝘂𝘁: undefined 𝗘𝘅𝗽𝗹𝗮𝗻𝗮𝘁𝗶𝗼𝗻: Inside test, this happens: 👉 Local x shadows global x 👉 And it’s initialized as undefined 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝟮: 𝘁𝗵𝗶𝘀 𝗕𝗶𝗻𝗱𝗶𝗻𝗴 (𝗥𝗲𝗮𝗹 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗙𝗮𝘃𝗼𝗿𝗶𝘁𝗲) 𝑐𝑜𝑛𝑠𝑡 𝑢𝑠𝑒𝑟 = {   𝑛𝑎𝑚𝑒: "𝑆ℎ𝑢𝑏ℎ𝑎𝑚",   𝑔𝑟𝑒𝑒𝑡() {    𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔(𝑡ℎ𝑖𝑠.𝑛𝑎𝑚𝑒);   } }; 𝑐𝑜𝑛𝑠𝑡 𝑔𝑟𝑒𝑒𝑡𝐹𝑛 = 𝑢𝑠𝑒𝑟.𝑔𝑟𝑒𝑒𝑡; 𝑔𝑟𝑒𝑒𝑡𝐹𝑛(); 𝗢𝘂𝘁𝗽𝘂𝘁: undefined 𝗘𝘅𝗽𝗹𝗮𝗻𝗮𝘁𝗶𝗼𝗻: • Function is called without object context • this → global object (or undefined in strict mode) 👉 this depends on how function is called, not where it's defined 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝟯: == 𝘃𝘀 === 𝗠𝗶𝗻𝗱 𝗧𝗿𝗮𝗽 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔(𝑛𝑢𝑙𝑙 == 𝑢𝑛𝑑𝑒𝑓𝑖𝑛𝑒𝑑); 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔(𝑛𝑢𝑙𝑙 === 𝑢𝑛𝑑𝑒𝑓𝑖𝑛𝑒𝑑); 𝗢𝘂𝘁𝗽𝘂𝘁: true, false 𝗘𝘅𝗽𝗹𝗮𝗻𝗮𝘁𝗶𝗼𝗻: • == → loose equality (special rule: null & undefined are equal) • === → strict equality (type must match) 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝟰: 𝗔𝗿𝗿𝗮𝘆 𝗟𝗲𝗻𝗴𝘁𝗵 𝗧𝗿𝗮𝗽 𝑐𝑜𝑛𝑠𝑡 𝑎𝑟𝑟 = [1, 2, 3]; 𝑎𝑟𝑟.𝑙𝑒𝑛𝑔𝑡ℎ = 0; 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔(𝑎𝑟𝑟); 𝗢𝘂𝘁𝗽𝘂𝘁: [] 𝗘𝘅𝗽𝗹𝗮𝗻𝗮𝘁𝗶𝗼𝗻: Setting length = 0 clears the array 👉 This is actually used in real apps for quick reset 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝟱: 𝗣𝗿𝗼𝗺𝗶𝘀𝗲 + 𝗘𝗿𝗿𝗼𝗿 𝗛𝗮𝗻𝗱𝗹𝗶𝗻𝗴 𝑃𝑟𝑜𝑚𝑖𝑠𝑒.𝑟𝑒𝑗𝑒𝑐𝑡("𝐸𝑟𝑟𝑜𝑟")   .𝑡ℎ𝑒𝑛(() => 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔("𝑆𝑢𝑐𝑐𝑒𝑠𝑠"))   .𝑐𝑎𝑡𝑐ℎ((𝑒𝑟𝑟) => {    𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔(𝑒𝑟𝑟);    𝑟𝑒𝑡𝑢𝑟𝑛 "𝑅𝑒𝑐𝑜𝑣𝑒𝑟𝑒𝑑";   })   .𝑡ℎ𝑒𝑛((𝑟𝑒𝑠) => 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔(𝑟𝑒𝑠)); 𝗢𝘂𝘁𝗽𝘂𝘁: Error Recovered 𝗘𝘅𝗽𝗹𝗮𝗻𝗮𝘁𝗶𝗼𝗻: • Rejected promise skips .then() • .catch() handles error & returns new value • Next .then() receives that value 💬 𝗪𝗵𝗮𝘁 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄𝗲𝗿𝘀 𝗔𝗰𝘁𝘂𝗮𝗹𝗹𝘆 𝗖𝗵𝗲𝗰𝗸 They don’t expect you to memorize outputs. They check if you understand: 👉 Execution context 👉 Scope & hoisting 👉 this binding 👉 Async flow If you can explain these confidently… You’re already performing at a top 10% frontend level in interviews. ♻️ Save this for revision and repost to help someone preparing for frontend interviews. 🚀 Follow Shubham Kumar Raj for more deep-dive JavaScript content #javascript #frontenddeveloper #codinginterview #webdevelopment #programming #learnjavascript #interviewprep #CareerGrowth #SowftwareEngineering #ReactJS

To view or add a comment, sign in

Explore content categories