JavaScript Variables & Identifiers Interview Questions and Answers

Explain identifiers, declarations, and the rules that govern naming and storing values in JavaScript.

Frequently Asked Questions

Important questions are highlighted in the sections below; select one to jump to its answer.

Theoretical JavaScript Interview Questions

In JavaScript, naming variables has a few rules you need to follow, and breaking them will cause errors:

  • 1. Must start with a letter, underscore (_), or dollar sign ($). Starting with a number is a syntax error.
  • 2. Can only contain letters, digits, underscores, or dollar signs after the first character.
  • 3. Case-sensitive: name and Name are different variables.
  • 4. Can't use reserved words like let, class, or return as variable names.
  • 5. Use descriptive names. userAge beats x every time. Your future self (and your teammates) will thank you.

Identifiers are simply the names you give things in your code - variables, functions, classes, parameters. Think of them as labels you stick on boxes so you can find them later. Every time you write let userName or function calculateTotal(), those names are identifiers. Choosing clear, descriptive identifiers is one of the simplest ways to make your code readable without needing comments.

JavaScript has a few hard rules for identifiers : they must start with a letter, underscore, or dollar sign (never a number), they're case-sensitive, and they can't be reserved keywords. Beyond the rules, conventions matter: use camelCase for variables and functions, PascalCase for classes, and UPPER_SNAKE_CASE for constants. Following these patterns makes your code instantly recognizable to other JavaScript developers. Read More

A value is the actual data - a number, string, boolean, object, etc. A variable is a named label you attach to a value so you can reference and reuse it. Think of a variable as a box with a name on it; the value is what's inside the box - and with let you can swap what's inside, while const locks the box shut.

Use const by default for bindings that are not reassigned and let when reassignment is needed. They have block scope, unlike var function scope.

Identifiers can use letters, underscore, dollar sign, and digits after the first character, but cannot begin with a digit or be a reserved word in the relevant context.

No. const prevents rebinding the variable, but the object properties can still change unless separately prevented or treated immutably.

`let` can be assigned a new value but cannot be declared twice in the same scope. `const` cannot be reassigned and needs an initial value, while `var` can be redeclared in its function or global scope.

A var binding is created for its containing function or global scope before execution and initially has value undefined; the assignment remains at its original execution point.

Top-level var declarations in classic scripts generally create properties on the global object, while top-level let bindings do not. Module scope has different global-binding behavior, so avoid relying on accidental globals.

A function retains access to the lexical environment where it was created, keeping referenced bindings available for later calls.

A let declaration in a loop creates a per-iteration binding, while var shares one function-scoped binding, so deferred callbacks can observe different values.

An inner-scope binding with the same name hides an outer binding within that scope. Distinct, descriptive names and narrow scopes make references easier to reason about.

A const binding prevents accidental rebinding, and limiting a variable scope limits where its value can change. Neither makes referenced objects immutable, so protect mutable state separately when required.

JavaScript Practical and Coding Interview Questions

Problem: Declare a const array of three prices and a let total, compute their sum with a loop, and log total. Keep every binding inside a function.

javascript

function calculateTotal() {
  const prices = [4, 7, 9];
  let total = 0;
  for (const price of prices) {
    total += price;
  }
  console.log(total);
}

calculateTotal();