Answer:
- Primitive types:
String
,Number
,Boolean
,Undefined
,Null
,Symbol
,BigInt
. - Non-primitive type:
Object
(includes arrays, functions, etc.).
Answer:
var
: Function-scoped, can be re-declared, and hoisted.let
: Block-scoped, cannot be re-declared, and allows reassignment.const
: Block-scoped, cannot be re-declared or reassigned.
Answer:
==
: Checks for equality after type coercion (loose equality).===
: Checks for equality without type coercion (strict equality).
Answer:
A closure is a function that retains access to its outer scope, even after the outer function has finished executing.
Example:
function outer() {
let count = 0;
return function inner() {
count++;
return count;
};
}
const increment = outer();
console.log(increment()); // 1
console.log(increment()); // 2
Answer:
undefined
: A variable that has been declared but not assigned a value.null
: An explicitly assigned value representing no value or absence of an object.
Answer:
- Event Bubbling: Events propagate from the innermost element (target) to the outermost (parent).
- Event Capturing: Events propagate from the outermost element (parent) to the innermost (target).
console.log(0.1 + 0.2 === 0.3);
Answer:
false
Explanation: Due to floating-point precision errors, 0.1 + 0.2
results in 0.30000000000000004
.
Answer:
call()
: Calls a function with a giventhis
value and arguments passed individually.
Example:func.call(thisArg, arg1, arg2)
.apply()
: Similar tocall()
but takes arguments as an array.
Example:func.apply(thisArg, [arg1, arg2])
.bind()
: Returns a new function with a giventhis
value and arguments pre-set.
Example:const boundFunc = func.bind(thisArg, arg1)
.
Answer:
Arrow functions are a concise way to write functions.
Key differences:
- Syntax:
const func = () => { ... }
. this
binding: Arrow functions do not bind their ownthis
; they inherit it from the surrounding context.- Cannot be used as constructors.
let a = [];
let b = [];
console.log(a == b, a === b);
Answer:
false false
Explanation: Arrays are objects, and object comparison checks for reference equality. Since a
and b
are different objects, they are not equal.
These questions cover essential JavaScript concepts and should give you a good sense of the rookie's understanding.