What is JavaScript?
JavaScript is a high-level, dynamically typed programming language primarily used to make web pages interactive. It runs natively in web browsers and can also run outside the browser through environments such as Node.js.
JavaScript interview questions covering language fundamentals, ECMAScript, data types, scope, hoisting, coercion, functions, parameters, spread and destructuring.
Open a question for the full answer, code and interview guidance.
JavaScript is a high-level, dynamically typed programming language primarily used to make web pages interactive. It runs natively in web browsers and can also run outside the browser through environments such as Node.js.
ECMAScript is the standardized language specification on which JavaScript is based. It defines the syntax, types, operators, objects, statements, functions, and other core language behavior.
JavaScript has seven primitive data types: string, number, bigint, boolean, undefined, symbol, and null. Primitive values are not objects and are immutable, meaning the primitive value itself cannot be changed in place.
Primitive values such as strings, numbers, and booleans are copied as values. Objects, arrays, and functions are reference types in practical JavaScript terminology: a variable stores a reference to the object rather than embedding a separate copy of the entire object.
var is function-scoped, can be redeclared in the same scope, and its declaration is hoisted and initialized with undefined. let and const are block-scoped and cannot be redeclared in the same block.
Scope determines where a variable or function can be accessed. JavaScript commonly has global scope, function scope, block scope, and module scope.
Lexical scope means that variable accessibility is determined by where functions and blocks are written in the source code. An inner function can access variables from its own scope and from its enclosing scopes.
Hoisting describes how JavaScript processes declarations before executing code in a scope. Function declarations can generally be called before their textual declaration. var declarations are hoisted and initialized with undefined.
The Temporal Dead Zone is the period between entering a block scope and executing the declaration of a let, const, or class binding. During this period, the binding exists but cannot be accessed.
The == operator performs loose equality comparison and may convert operands to compatible types before comparing them. The === operator performs strict equality and does not perform this type coercion.
Type coercion is the conversion of a value from one type to another. JavaScript can perform implicit coercion automatically during certain operations, or developers can perform explicit conversion using functions such as Number, String, and Boolean.
In a Boolean context, JavaScript converts values to true or false. Falsy values include false, 0, -0, 0n, an empty string, null, undefined, and NaN. Most other values are truthy, including empty arrays and empty objects.
undefined usually represents a value that has not been assigned or is missing, while null is normally used intentionally to represent the absence of a value.
NaN means Not-a-Number. It is a special numeric value representing an invalid or unrepresentable numeric result, such as trying to convert non-numeric text to a number.
Number.isNaN(value) returns true only when the supplied value is actually the numeric NaN value. The global isNaN(value) first attempts numeric coercion and then tests the result.
Template literals are string literals written with backticks. They support embedded expressions through ${...}, multi-line strings, and tagged template functionality.
A function declaration defines a named function using the function keyword. Function declarations are hoisted in a way that normally allows them to be called before the declaration appears in the source code.
A function expression creates a function as part of an expression, commonly by assigning it to a variable. The function may be anonymous or named.
A function declaration is declared directly with the function keyword and a name, while a function expression creates a function as a value inside an expression, often assigned to a variable.
An arrow function is a compact function syntax introduced in ES6. It is especially convenient for callbacks and short functional operations.
A regular function's this value generally depends on how the function is called. An arrow function does not define its own this; instead, it captures this lexically from the surrounding scope.
Default parameters allow a function parameter to receive a predefined value when the corresponding argument is omitted or explicitly passed as undefined.
Rest parameter syntax collects zero or more remaining arguments into a real array. It is written with three dots before the final parameter name.
Spread syntax uses three dots to expand an iterable into individual elements or to copy enumerable properties into a new object. It is commonly used with arrays, function arguments, and object literals.
Destructuring is syntax for extracting values from arrays or properties from objects into variables. It supports defaults, renaming, nested patterns, and rest elements.