Progressive Full Stack Application Development with Live Projects

JavaScript Variables & Operators Interview Questions
JavaScript Interview

JavaScript Variables, Operators and Control Flow Interview Questions

Any software project at the core is a collection of values stored in something called Variables in Programming language. Here’s a collection of basic interview questions & answers related to JavaScript variables, data types and common operators that are used to perform operations on these variables.


1. What are the different data types in JavaScript?

JavaScript has 7 data types:

  • Number: Represents numeric values (e.g., 42, 3.14).
  • String: Represents sequences of characters (e.g., "hello").
  • Boolean: Represents true or false.
  • Undefined: A variable that has been declared but has not been assigned a value.
  • Null: Represents the intentional absence of any value.
  • Symbol: A unique and immutable value primarily used as object property keys (introduced in ES6).
  • BigInt: Represents integers with arbitrary precision (e.g., 1234567890123456789012345678901234567890n).

Object Type: A collection of key-value pairs (e.g., { name: "Alice", age: 30 }).


2. How can you check the type of a variable in JavaScript?

We can use the typeof operator. For example:

let x = 5;
console.log(typeof x); // "number"

3. What is the difference between null and undefined?

undefined is a type itself (and a value) that indicates a variable has been declared but not assigned a value

null is an object that represents the intentional absence of any value. It’s often used to indicate that a variable should be empty.


4. How can you declare variables in JavaScript?

We can declare variables using:

  • var: Function-scoped or globally scoped (not recommended due to hoisting issues).
  • let: Block-scoped and allows reassignment.
  • const: Block-scoped and does not allow reassignment (but objects can be mutated).

5. What is hoisting in JavaScript?

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their containing scope during the compile phase. Only the declarations are hoisted, not the initializations. For example:

console.log(x); // undefined
var x = 5;

6. What is the difference between let, const, and var?

  • var is function-scoped and allows redeclaration and reassignment.
  • let is block-scoped and allows reassignment but not redeclaration within the same block.
  • const is block-scoped and does not allow reassignment or redeclaration. However, it can hold mutable objects.

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

  • == is the equality operator that performs type coercion if the types of the operands are different.
  • === is the strict equality operator that checks both value and type without type coercion.
console.log(5 == '5');  // true
console.log(5 === '5'); // false

8. What are logical operators in JavaScript?

The logical operators include:

  • && (AND) : Returns true if both operands are true.
  • || (OR): Returns true if at least one operand is true.
  • ! (NOT): Inverts the truthiness of the operand.

9. What is the ternary operator?

The ternary operator is a shorthand for an if...else statement. It has the syntax

condition ? expressionIfTrue : expressionIfFalse;

Example

const age = 18;
const isAdult = age >= 18 ? "Adult" : "Minor";

10. What is short-circuit evaluation?

Short-circuit evaluation refers to the way logical operators work. For &&, if the first operand is false, the second operand is not evaluated because the overall expression cannot be true. For ||, if the first operand is true, the second is not evaluated because the overall expression is already true.