JavaScript Trickiest Questions
� JavaScript’s Trickiest Questions for Advanced Developers 🧩: Crack the Code Like a Pro! 🚀
JavaScript is a powerful and versatile language, but it can also be tricky, especially for advanced developers who dive deep into its nuances. Whether you’re preparing for a technical interview or just want to level up your skills, understanding these tricky questions will help you write cleaner, more efficient code. Let’s explore some of the trickiest JavaScript questions, break them down with examples, and share tips to write pro-level code! 💡
1. What’s the Output of typeof null
? 🤔
Question:
console.log(typeof null);
Answer:
The output is "object"
. 😮
Explanation:
This is a long-standing bug in JavaScript that dates back to its early days. The typeof
operator incorrectly identifies null
as an object. To check for null
, use:
console.log(myVar === null); // true if myVar is null
2. Why Does 0.1 + 0.2 !== 0.3
? 🧮
Question:
console.log(0.1 + 0.2 === 0.3); // false
Answer:
Due to floating-point precision issues, 0.1 + 0.2
equals 0.30000000000000004
, not 0.3
.
Explanation:
JavaScript uses binary floating-point arithmetic, which can’t precisely represent decimal fractions. To handle this, use Number.EPSILON
for comparisons:
console.log(Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON); // true
3. What’s the Difference Between ==
and ===
? 🤨
Question:
console.log(1 == "1"); // true
console.log(1 === "1"); // false
Answer:
==
performs type coercion, converting values to the same type before comparison.===
checks both value and type without coercion.
Pro Tip:
Always use ===
to avoid unexpected behavior caused by type coercion. 🛑
4. What’s the Output of [] + []
? 🤯
Question:
console.log([] + []);
Answer:
The output is an empty string ""
.
Explanation:
When arrays are used with the +
operator, they are converted to strings. An empty array becomes an empty string, so [] + []
results in ""
.
5. What’s the Difference Between let
, var
, and const
? 📦
Question:
console.log(x); // undefined
var x = 10;
console.log(y); // ReferenceError
let y = 20;
Answer:
var
is function-scoped and hoisted.let
andconst
are block-scoped and not hoisted.const
cannot be reassigned after declaration.
Pro Tip:
Use const
by default, let
for variables that change, and avoid var
. 🚫
6. What’s the Output of setTimeout
Inside a Loop? ⏳
Question:
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1000);
}
Answer:
The output is 3, 3, 3
after 1 second.
Explanation:
var
is function-scoped, so by the time the setTimeout
callback runs, the loop has already completed, and i
is 3
.
Fix:
Use let
for block scoping:
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1000);
}
// Output: 0, 1, 2
7. What’s the Difference Between null
and undefined
? ❓
Question:
console.log(null == undefined); // true
console.log(null === undefined); // false
Answer:
null
is an intentional absence of value.undefined
means a variable has been declared but not assigned.
Pro Tip:
Use null
to explicitly indicate “no value” and undefined
to check if a variable is uninitialized. 🎯
8. What’s the Output of "5" + 3
? 🧐
Question:
console.log("5" + 3);
Answer:
The output is "53"
.
Explanation:
The +
operator performs string concatenation when one operand is a string. To add numbers, convert the string to a number first:
console.log(Number("5") + 3); // 8
9. What’s the Difference Between call
, apply
, and bind
? 📞
Question:
const obj = { value: 42 };
function getValue() {
return this.value;
}
console.log(getValue.call(obj)); // 42
console.log(getValue.apply(obj)); // 42
console.log(getValue.bind(obj)()); // 42
Answer:
call
andapply
invoke the function immediately, withcall
taking arguments individually andapply
taking them as an array.bind
returns a new function with the context bound.
Pro Tip:
Use bind
for partial application or to create reusable functions with a fixed context. 🔗
10. What’s the Output of Promise
Chaining? 🔄
Question:
Promise.resolve(1)
.then((x) => x + 1)
.then((x) => { throw new Error("Error!") })
.catch(() => 3)
.then((x) => x + 1)
.then(console.log);
Answer:
The output is 4
.
Explanation:
- The first
then
increments1
to2
. - The second
then
throws an error, which is caught bycatch
, returning3
. - The final
then
increments3
to4
.
🚀 Pro Tips for Writing Cleaner JavaScript Code 🧼
- Use ES6+ Features: Embrace
let
,const
, arrow functions, and destructuring for cleaner code. - Avoid Global Variables: Use modules and closures to limit scope.
- Lint Your Code: Use tools like ESLint to catch errors and enforce best practices.
- Write Pure Functions: Avoid side effects for predictable and testable code.
- Master Asynchronous Programming: Understand Promises,
async/await
, and event loops. - Optimize Performance: Use tools like Chrome DevTools to profile and optimize your code.
- Read the Docs: Stay updated with MDN Web Docs and ECMAScript specifications.
By mastering these tricky questions and following pro tips, you’ll not only ace interviews but also write robust, maintainable, and efficient JavaScript code. Happy coding! �✨
What’s your favorite JavaScript trick? Share in the comments below! 💬👇
© Lakhveer Singh Rajput - Blogs. All Rights Reserved.