Skip to content

Instantly share code, notes, and snippets.

@meetmakwana7396
Created January 9, 2025 04:02
Show Gist options
  • Save meetmakwana7396/56275f649853ef706eb716792954eae2 to your computer and use it in GitHub Desktop.
Save meetmakwana7396/56275f649853ef706eb716792954eae2 to your computer and use it in GitHub Desktop.

1. What are the different data types in JavaScript?

Answer:

  • Primitive types: String, Number, Boolean, Undefined, Null, Symbol, BigInt.
  • Non-primitive type: Object (includes arrays, functions, etc.).

2. Explain the difference between var, let, and const.

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.

3. What is the difference between == and ===?

Answer:

  • ==: Checks for equality after type coercion (loose equality).
  • ===: Checks for equality without type coercion (strict equality).

4. What is a closure in JavaScript?

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

5. What is the difference between undefined and null?

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.

6. What is event bubbling and event capturing?

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).

7. What is the output of the following code?

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.


8. Explain call(), apply(), and bind().

Answer:

  • call(): Calls a function with a given this value and arguments passed individually.
    Example: func.call(thisArg, arg1, arg2).
  • apply(): Similar to call() but takes arguments as an array.
    Example: func.apply(thisArg, [arg1, arg2]).
  • bind(): Returns a new function with a given this value and arguments pre-set.
    Example: const boundFunc = func.bind(thisArg, arg1).

9. What are arrow functions, and how are they different from regular functions?

Answer: Arrow functions are a concise way to write functions.
Key differences:

  1. Syntax: const func = () => { ... }.
  2. this binding: Arrow functions do not bind their own this; they inherit it from the surrounding context.
  3. Cannot be used as constructors.

10. What will the following code output and why?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment