JavaScript Keywords


JavaScript Keywords Interview with follow-up questions

1. What are JavaScript keywords and why are they important?

Keywords are reserved words with built-in meaning in the language — const, let, function, return, if, for, class, await, typeof, and so on. Because the parser gives them syntactic meaning, you can't use them as variable, function, or parameter names:

let class = 5; // SyntaxError: Unexpected reserved word

They matter because they define the grammar and control flow of every program — declaring bindings, branching, looping, defining classes, and managing async code.

Worth knowing for follow-ups:

  • Some words are contextual / reserved only in certain modesawait is reserved inside async functions and modules, yield inside generators, and let, static, implements are reserved only in strict mode (which modules and classes enable by default).
  • A few are reserved for future use (enum), and some old ones (with) are effectively dead in strict mode.
  • Values like undefined, NaN, and Infinity are not keywords — they're global properties, which is why older code could shadow them. Treat them as read-only.
↑ Back to top

Follow-up 1

Can you name a few JavaScript keywords?

Some of the JavaScript keywords include:

  • var: used to declare variables
  • if, else if, else: used for conditional statements
  • for, while, do while: used for loops
  • function: used to define functions
  • return: used to return a value from a function
  • break, continue: used to control the flow of loops
  • try, catch, finally: used for error handling
  • this: used to refer to the current object
  • new: used to create an instance of an object

Follow-up 2

What happens if you try to use a keyword as a variable name?

If you try to use a keyword as a variable name, you will get a syntax error. Keywords are reserved and cannot be used as identifiers for variables, functions, or any other named entities in JavaScript.

Follow-up 3

What is the 'this' keyword in JavaScript?

The 'this' keyword in JavaScript refers to the object that is currently executing the code. It is a special variable that is automatically defined in the scope of every function. The value of 'this' depends on how a function is called. When a function is called as a method of an object, 'this' refers to the object itself. When a function is called as a standalone function, 'this' refers to the global object (window in a browser). 'this' can also be explicitly set using the call(), apply(), or bind() methods.

Follow-up 4

What is the 'new' keyword used for in JavaScript?

The 'new' keyword in JavaScript is used to create an instance of an object. It is used with constructor functions to create new objects based on a blueprint defined by the constructor. When the 'new' keyword is used, it does the following:

  • Creates a new empty object
  • Sets the prototype of the new object to the prototype property of the constructor function
  • Executes the constructor function with 'this' set to the new object
  • Returns the new object

2. What is the purpose of the 'var' keyword in JavaScript?

var declares a variable the old way. It still works, but in modern code it's considered legacy — interviewers ask about it mainly to see if you know why let/const replaced it.

var has two defining behaviors:

  • Function scope (not block scope): a var inside an if or for block is visible throughout the whole enclosing function, which leaks beyond where you'd expect.
  • Hoisting with undefined: the declaration is hoisted to the top of its scope and initialized to undefined, so reading it before the line that assigns it returns undefined instead of throwing.
function demo() {
  console.log(x); // undefined (hoisted, no error)
  var x = 5;
}

The problems this causes:

  • It allows silent redeclaration (var x; var x;) and accidental globals — a top-level var attaches to the global object (window).
  • It's the source of the classic loop-closure bug, where every callback in a for (var i …) loop sees the final i.

Prefer const by default, let when reassigning — both are block-scoped and sit in the Temporal Dead Zone, so early access throws instead of silently giving undefined.

↑ Back to top

Follow-up 1

What is the difference between 'var', 'let', and 'const'?

In JavaScript, 'var', 'let', and 'const' are used to declare variables, but they have some differences:

  • 'var' has function scope, while 'let' and 'const' have block scope.
  • Variables declared with 'var' are hoisted to the top of their scope, while variables declared with 'let' and 'const' are not hoisted.
  • 'var' allows variable redeclaration within the same scope, while 'let' and 'const' do not allow redeclaration.
  • 'let' allows variable reassignment, while 'const' does not allow reassignment after it has been initialized.

Follow-up 2

What is hoisting in JavaScript?

Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase, before the code is executed. This means that you can use variables and functions before they are declared. However, only the declarations are hoisted, not the initializations. Variables declared with 'var' are hoisted, while variables declared with 'let' and 'const' are not hoisted.

Follow-up 3

Can you explain the scope of a variable declared with 'var'?

Variables declared with 'var' have function scope. This means that they are accessible within the function in which they are declared, as well as any nested functions. However, they are not accessible outside of the function. If a variable declared with 'var' is accessed before it is declared, it will have the value 'undefined'. If a variable with the same name is declared multiple times within the same scope, the later declaration will override the earlier one.

3. How does the 'function' keyword work in JavaScript?

The function keyword defines a function. It appears in two main forms, and the difference is mostly about hoisting:

// Function declaration — hoisted; callable before it appears
sum(2, 3); // 5 works
function sum(a, b) {
  return a + b;
}

// Function expression — assigned to a binding; NOT hoisted
const mul = function (a, b) {
  return a * b;
};

Follow-ups interviewers expect:

  • A declaration is hoisted entirely (name + body), so it can be called earlier in its scope. A function expression is only as available as its variable — with const/let it sits in the Temporal Dead Zone until the line runs.
  • Inside a regular function, this is dynamic — it depends on how the function is called (method call, plain call, call/apply/bind, or new). This is the main contrast with arrow functions, which have no own this.
  • Regular functions also get their own arguments object and can be used as constructors with new; arrow functions can do neither.
  • Prefixing it gives variants: async function, function* (generator), and async function*.
↑ Back to top

Follow-up 1

What is a function declaration?

A function declaration is a way to define a named function using the 'function' keyword followed by the function name, a list of parameters (optional), and the function body enclosed in curly braces. Here's an example:

function greet(name) {
    console.log('Hello, ' + name + '!');
}

Follow-up 2

What is a function expression?

A function expression is a way to define a function as a value assigned to a variable. It starts with the 'function' keyword, followed by an optional function name (known as a named function expression), a list of parameters (optional), and the function body enclosed in curly braces. Here's an example:

var greet = function(name) {
    console.log('Hello, ' + name + '!');
};

Follow-up 3

Can you explain the concept of 'function hoisting'?

Function hoisting is a JavaScript behavior where function declarations are moved to the top of their containing scope during the compilation phase, before the code is executed. This means that you can call a function before it is declared in the code. However, it's important to note that function expressions are not hoisted. Here's an example:

// Function declaration
greet('John');

function greet(name) {
    console.log('Hello, ' + name + '!');
}

// Function expression
sayHello('Jane'); // Error: sayHello is not defined

var sayHello = function(name) {
    console.log('Hello, ' + name + '!');
};

4. What is the 'return' keyword in JavaScript?

return ends a function's execution and hands a value back to the caller. If you omit it (or write bare return;), the function returns undefined.

function add(a, b) {
  return a + b;
}

const result = add(2, 3);
console.log(result); // 5

Things interviewers like to surface:

  • Code after a return in the same block is unreachable — it never runs.
  • A function can have multiple returns for early exits (guard clauses), which often reads cleaner than nested ifs.
  • Automatic semicolon insertion gotcha: never put a newline right after return. This returns undefined, not the object, because ASI inserts a semicolon after return:
  function broken() {
    return
      { value: 1 }; // unreachable; function returns undefined
  }
  • Arrow functions with a concise body have an implicit return: const add = (a, b) => a + b; (to return an object literal, wrap it in parens: () => ({ x: 1 })).
↑ Back to top

Follow-up 1

What happens if a function doesn't have a return statement?

If a function doesn't have a 'return' statement, it will still execute the code inside the function but it will not return any value. In this case, the function will return 'undefined' by default. Here's an example:

function greet(name) {
  console.log('Hello, ' + name + '!');
}

var result = greet('John');
console.log(result); // Output: undefined

In this example, the 'greet' function does not have a 'return' statement, so it returns 'undefined' when called.

Follow-up 2

Can a function return multiple values?

No, a function in JavaScript cannot directly return multiple values. However, you can return multiple values by using an array or an object. Here's an example using an array:

function getFullName(firstName, lastName) {
  return [firstName, lastName];
}

var fullName = getFullName('John', 'Doe');
console.log(fullName[0]); // Output: John
console.log(fullName[1]); // Output: Doe

In this example, the 'getFullName' function returns an array containing the first name and last name.

Follow-up 3

What is the purpose of 'return' in a recursive function?

In a recursive function, the 'return' statement is used to end the execution of the current recursive call and return a value to the previous recursive call. This is important because it allows the function to build up the final result by combining the values returned from each recursive call. Here's an example of a recursive function to calculate the factorial of a number:

function factorial(n) {
  if (n === 0) {
    return 1;
  }
  return n * factorial(n - 1);
}

var result = factorial(5);
console.log(result); // Output: 120

In this example, the 'return' statement is used to return the product of 'n' and the result of the recursive call to 'factorial(n - 1)'.

5. What does the 'typeof' keyword do in JavaScript?

typeof is a unary operator that returns a string naming the type of its operand:

typeof 42;          // "number"
typeof "hello";     // "string"
typeof true;        // "boolean"
typeof 10n;         // "bigint"
typeof Symbol();    // "symbol"
typeof undefined;   // "undefined"
typeof {};          // "object"

The gotchas interviewers fish for:

  • typeof null === "object" — a famous legacy bug, not a real "null type". To test for null, compare directly: value === null.
  • typeof function(){} === "function" — even though functions are objects, typeof singles them out.
  • Arrays and most objects all report "object", so use Array.isArray(x) for arrays.
  • typeof someUndeclaredVar returns "undefined" without throwing — the one safe way to reference a name that might not exist (handy for feature detection). Note this trick fails for let/const in the TDZ, which throw.

For a more specific tag, Object.prototype.toString.call(x) gives results like "[object Array]" or "[object Date]".

↑ Back to top

Follow-up 1

What are the possible return values of 'typeof'?

The possible return values of 'typeof' are: 'undefined', 'boolean', 'number', 'string', 'bigint', 'symbol', 'function', and 'object'.

Follow-up 2

What is the result of 'typeof null'?

The result of 'typeof null' is 'object'. This is a historical bug in JavaScript and is considered a mistake. 'null' is actually a primitive value, but due to an implementation quirk, 'typeof null' returns 'object'.

Follow-up 3

How can 'typeof' be used to check if a variable is an array?

The 'typeof' operator cannot directly determine if a variable is an array. It will return 'object' for arrays as well. To check if a variable is an array, you can use the 'Array.isArray()' method. For example:

const arr = [1, 2, 3];
console.log(Array.isArray(arr)); // Output: true

6. What is hoisting, and how does the Temporal Dead Zone relate to it?

Hoisting is JavaScript's behavior of processing declarations before executing code, so they're usable within their scope — but the details differ by declaration type, which is the real interview content.

  • var — declaration hoisted and initialized to undefined, so reading it before assignment returns undefined (no error).
  • let/const — also hoisted, but not initialized; they sit in the Temporal Dead Zone (TDZ) from the top of the block until the declaration line. Accessing them in the TDZ throws a ReferenceError.
  • Function declarations — fully hoisted (name and body), so you can call them before they appear.
  • Function/arrow expressions — only the variable is hoisted (per var/let rules), not the function value.
console.log(a); // undefined
var a = 1;
console.log(b); // ReferenceError (TDZ)
let b = 2;
greet();        // works — function declaration hoisted
function greet() {}

Follow-ups: the TDZ is why let/const catch use-before-declaration bugs that var silently hid; class declarations are also TDZ-bound (not callable before declaration); and hoisting operates per-scope (function/block), not across the whole file.

↑ Back to top

Live mock interview

Mock interview: JavaScript Keywords

Intermediate ~5 min Your own free AI key

Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.