From bd11515b50a43ade3e166423e080563026baee41 Mon Sep 17 00:00:00 2001 From: nishant borude Date: Mon, 13 Oct 2025 15:59:23 +0530 Subject: [PATCH] ninja question --- README.md | 211 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) diff --git a/README.md b/README.md index 3bc019c..c9c91bc 100644 --- a/README.md +++ b/README.md @@ -4073,6 +4073,217 @@ setTimeout(() => { },2000) ``` +# πŸ₯· Ninja-Level JavaScript Questions + +A collection of tricky, edge-case, and uncommon JavaScript questions for advanced learners and interview prep. These questions are designed to test deep understanding and β€œgotchas” in JavaScript. + +--- + +## Questions + +1. Why does this print `undefined`? +```js +console.log(a); +var a = 10; + + +Answer: +var declarations are hoisted to the top of their scope. The declaration (var a) is hoisted, but the assignment (a = 10) is not. So at the time of console.log(a), a exists but is undefined. +------------------------------------------- +2.Why does [] == ![] evaluate to true? +Answer: + +![] converts the array to boolean β†’ true, then negates β†’ false. + +[] == false triggers type coercion: array β†’ primitive string β†’ "", then "" == false β†’ true. + +------------------------------------------- +3.Difference between NaN === NaN and Object.is(NaN, NaN). +Answer: + +NaN === NaN β†’ false because NaN is never equal to itself. + +Object.is(NaN, NaN) β†’ true because Object.is treats NaN as equal to itself. +------------------------------------------- +4.What will this print and why? +for (var i = 0; i < 3; i++) { + setTimeout(() => console.log(i), 1000); +} +var is function-scoped, so all timeouts share the same i, which is 3 after the loop. +Using let i would print 0,1,2 because let is block-scoped. +------------------------------------------- +5.Explain why typeof null returns "object". +Answer: +A historical bug in JavaScript. null is a primitive, but typeof null incorrectly returns "object" for legacy reasons. +------------------------------------------- +6.What will this print? +console.log([] + {}); +console.log({} + []); +Answer: + +[] + {} β†’ "" + "[object Object]" β†’ "[object Object]" + +{} can be interpreted as a block; adding + [] triggers coercion β†’ context matters +------------------------------------------- +7.Explain sparse arrays: +let a = [1,2,3]; +a[10] = 99; +console.log(a.length); +Answer: +a.length β†’ 11. Arrays in JS are sparse, meaning indices 3–9 are empty but counted in .length. +------------------------------------------- +8.Why does 0.1 + 0.2 === 0.3 return false? +Answer: +Floating-point precision error. 0.1 + 0.2 = 0.30000000000000004. +------------------------------------------- +9.Difference between == and === when comparing null and undefined. +Answer: + +null == undefined β†’ true (type coercion) + +null === undefined β†’ false (strict equality, no coercion) +------------------------------------------- +10.What happens here? +let a = [1,2,3]; +a[5] = undefined; +console.log(a.length); // 6 +console.log(a); // [1,2,3,undefined Γ— 2, undefined] + +Answer: +Assigning to index 5 creates sparse elements (index 3 and 4 are empty), .length counts all positions. +------------------------------------------- +11.Why does this work? +(() => {}) instanceof Function; // true + +Answer: +Arrow functions are special kinds of functions, so they are instances of Function. +------------------------------------------- +12.What happens if you do: +delete Array.prototype.push; + +Answer: +Removes the push method from all arrays. After this, [].push() will throw an error. +------------------------------------------- +13.Difference between call, apply, and bind + +Answer: + +call(thisArg, arg1, arg2, ...) β†’ invokes function immediately. + +apply(thisArg, [args]) β†’ like call but takes args as array. + +bind(thisArg, args) β†’ returns a new function with bound this. +------------------------------------------- +14.What will this print? +console.log([] == false); // true +console.log([] === false); // false + +Answer: +== triggers coercion: [] β†’ "", then "" == false β†’ true. === checks type β†’ array !== boolean. +------------------------------------------- +15.Explain this behavior in arrow functions vs regular functions + +Answer: + +Regular function: this depends on how function is called. + +Arrow function: this is lexically inherited from enclosing scope. +------------------------------------------- +16.How do var, let, const behave differently in loops with async callbacks? + +Answer: + +var β†’ function-scoped β†’ same variable for all iterations β†’ closure issues. + +let/const β†’ block-scoped β†’ each iteration has its own variable β†’ works as expected. +------------------------------------------- +17.What is the output and why? +console.log('5' - 3); // 2 +console.log('5' + 3); // "53" + +Answer: + +- triggers numeric coercion β†’ '5' β†’ 5 + ++ triggers string concatenation β†’ '5' + '3' = "53" + +------------------------------------------- +18.. Explain event loop order with setTimeout vs Promise.resolve().then() + +Answer: + +Promises are microtasks, executed before the next macrotask (setTimeout). + +Example:setTimeout(() => console.log('timeout'), 0); +Promise.resolve().then(() => console.log('promise')); +// Output: promise, timeout + +------------------------------------------- +19.What will this print? +console.log(typeof NaN); // "number" +console.log(typeof Infinity); // "number" + +Answer: +Both NaN and Infinity are number primitives. +------------------------------------------- +20.. Explain prototypal inheritance and difference between Object.create() and new + +Answer: + +Object.create(proto) β†’ creates object with specified prototype, no constructor call. + +new Constructor() β†’ creates object, sets prototype, runs constructor. + +------------------------------------------- + +21.Why does this happen? +console.log(true + false); // 1 + +Answer: +Boolean values are converted to numbers: true β†’ 1, false β†’ 0 +------------------------------------------- +22.What will this print? +console.log([] + []); // "" +console.log([] + {}); // "[object Object]" +Answer: + +[] + [] β†’ empty string + +[] + {} β†’ "" + "[object Object]" β†’ "[object Object]" +------------------------------------------- +23.Explain short-circuit evaluation in: +true || false && false; // true + +Answer: + +&& has higher precedence β†’ false && false β†’ false + +true || false β†’ true +------------------------------------------- +24.What is the output and why? +console.log(1 < 2 < 3); // true +console.log(3 > 2 > 1); // false +Answer: + +1 < 2 β†’ true β†’ true < 3 β†’ true coerced to 1 β†’ 1 < 3 β†’ true + +3 > 2 β†’ true β†’ true > 1 β†’ 1 > 1 β†’ false +------------------------------------------- +25.Difference between primitive and reference types in JS assignments + +Answer: + +Primitive (number, string, boolean, null, undefined, symbol) β†’ copied by value. + +Reference (objects, arrays, functions) β†’ copied by reference; changes affect original. + + + + + + + +
Answer The above program will print 0 to 9 sequentially.