February 20, 2025

Javascript Interview Question

javascript interview question
What is JavaScript?

JavaScript is a high-level, interpreted programming language used to create dynamic and interactive web content.

JavaScript supports the following data types:

  1. Primitive: string, number, boolean, null, undefined, symbol, bigint
  2. Non-Primitive: object
  • var: Scope can be re-declared and hoisted.
  • let: Block-scoped, can’t be re-declared, can’t be hoisted.
  • const: Block-scoped, immutable, not hoisted.
  • ==: Compares values after type conversion (loose equality).
  • ===: Compares values without type conversion (strict equality).

The isNaN() function checks whether a value is NaN (Not-a-Number).

  • null: Represents the intentional absence of a value.
  • undefined: Represents a variable that has been declared but not assigned a value.

Hoisting is JavaScript’s behavior of moving declarations (var, function) to the top of their scope during compilation.

A closure is a function that has access to its outer function’s variables even after the outer function has executed.

  • Function Declaration: Defined with the function keyword and hoisted.
  • Function Expression: Assigned to a variable and not hoisted.

Promises are used to handle asynchronous operations. They can be in one of three states: pending, fulfilled, or rejected.

  • call(): Invokes a function with a specified this context and arguments.
  • apply(): Similar to call(), but takes arguments as an array.
  • bind(): Returns a new function with the specified this context.

The event loop is a mechanism that ensures non-blocking execution by handling asynchronous operations and executing callbacks.

A higher-order function is a function that takes another function as an argument or returns a function.

  • map(): Creates a new array by applying a function to each element.
  • filter(): Creates a new array with elements that pass a condition.
  • reduce(): Reduces an array to a single value by applying a function.

The value of this depends on how a function is called:

  • In a method, this refers to the object.
  • In a regular function, this refers to the global object (window in browsers).
  • Synchronous: Executes code sequentially, blocking further execution.
  • Asynchronous: Executes code without blocking, using callbacks, promises, or async/await.

async functions return promises. await pauses execution until the promise is resolved.

Destructuring is a syntax for extracting values ​​from arrays or objects:

				
					const {name, age} = person;
const [first, second] = array;
				
			

Templates allow to embed variables and expressions using literal backticks () and ${}` syntax:

				
					const greeting = `Hello, ${name}!`;
				
			
  • undefined: A variable is declared but not assigned a value.
  • not defined: A variable is not declared in the program.

Prototype is an object associated with every function and object, which is used for inheritance.

  • setTimeout: Executes a function after a delay.
  • setInterval: Executes a function repeatedly at a specified interval.