Essential JavaScript Interview Questions — Questions & Answers

Challenge your JavaScript fundamentals with 15 essential interview questions covering core concepts like variables, functions, data types, and scope. Gain confidence in JavaScript basics and key features common in technical interviews.

This quiz contains 16 questions. Below is a complete reference of all questions, answer choices, and correct answers. You can use this section to review after taking the interactive quiz above.

  1. Question 1: JavaScript Definition

    Which description best defines JavaScript as a programming language?

    • A high-level, interpreted scripting language mainly used for web development.
    • A database query language for server-side applications.
    • A strongly typed, compiled language designed for embedded systems.
    • A spreadsheet formula language used in office software.
    Show correct answer

    Correct answer: A high-level, interpreted scripting language mainly used for web development.

    Explanation: JavaScript is a high-level, interpreted language widely used for creating interactive web pages and applications. It runs primarily in browsers but is also used on servers. The other options describe different types of languages or tools that are not accurate descriptions of JavaScript. For example, the second option refers to SQL, the third to languages like C, and the fourth to spreadsheet formulas.

  2. Question 2: Variable Declaration Keywords

    What is the main difference between let and var when declaring variables in JavaScript?

    • let is block-scoped while var is function-scoped.
    • let can be redeclared in the same scope while var cannot.
    • let is only available in older browsers while var is modern.
    • let declarations are hoisted but var declarations are not.
    Show correct answer

    Correct answer: let is block-scoped while var is function-scoped.

    Explanation: The primary difference is that let is block-scoped and var is function-scoped, meaning let variables exist only in the nearest enclosing block, while var variables are available throughout their containing function. The second option is incorrect; var can be redeclared but let cannot in the same scope. The third option is false, as var is older. The fourth option reverses the actual hoisting behavior.

  3. Question 3: Const Keyword

    Which statement about const declarations in JavaScript is correct?

    • Variables declared with const cannot be reassigned.
    • const allows reassigning new values to the variable.
    • const is function-scoped like var.
    • const variables are always undefined.
    Show correct answer

    Correct answer: Variables declared with const cannot be reassigned.

    Explanation: The correct answer is that variables declared with const cannot be reassigned to a new value, ensuring their reference remains constant. The second option is false because const does not allow reassignment. The third is incorrect, as const is block-scoped. The fourth option is wrong because const variables must be initialized at declaration and are not always undefined.

  4. Question 4: Data Types

    Which of the following is NOT a primitive data type in JavaScript?

    • Object
    • String
    • Boolean
    • Number
    Show correct answer

    Correct answer: Object

    Explanation: Objects are not primitive data types in JavaScript; they are complex structures. The other options—String, Boolean, and Number—are all valid primitive data types. This distinction is key in understanding how data is stored and manipulated in JavaScript.

  5. Question 5: Function Declaration

    How do you correctly declare a named function in JavaScript?

    • function greet() { return 'Hello'; }
    • define greet() { return 'Hello'; }
    • func greet[] { return 'Hello'; }
    • greet = function() ['Hello'];
    Show correct answer

    Correct answer: function greet() { return 'Hello'; }

    Explanation: The standard way to declare a named function in JavaScript uses the function keyword followed by the function name, as in the correct answer. The other options use incorrect syntax or keywords not found in JavaScript. For instance, 'define' and 'func' are not valid, and using brackets as in the last option is also invalid.

  6. Question 6: Arrow Functions

    Which syntax creates an arrow function that returns the sum of two numbers?

    • (a, b) => a + b
    • (a b) => { a + b }
    • arrow(a, b) (a + b)
    • {a, b} -> a plus b
    Show correct answer

    Correct answer: (a, b) => a + b

    Explanation: The correct syntax for an arrow function in JavaScript is (a, b) => a + b. The next two options use invalid function syntax, either missing commas or using undefined keywords. The last option uses incorrect symbols and simplistic English instead of code, which is not valid in JavaScript.

  7. Question 7: Strict Equality

    What does the '===' operator check for in JavaScript?

    • Both strict value and type equality.
    • Only value equality, not type.
    • Only type match, not value.
    • Inequality between operands.
    Show correct answer

    Correct answer: Both strict value and type equality.

    Explanation: The strict equality operator '===' checks for equality of both value and data type. The second option refers to '==' which checks only value. The third is incorrect because '===' checks both, not just type. The fourth refers to operators like '!='.

  8. Question 8: Typeof Operator

    What is the result of typeof undefined in JavaScript?

    • 'undefined'
    • 'null'
    • 'object'
    • 'string'
    Show correct answer

    Correct answer: 'undefined'

    Explanation: When using typeof undefined, the result is the string 'undefined'. 'null' would return 'object' instead. The options 'object' and 'string' are incorrect because only undefined produces the provided result when passed to typeof.

  9. Question 9: Template Literals

    Which syntax uses a template literal to include the variable name in the output?

    • `Hello, ${name}!`
    • 'Hello, $name!'
    • "Hello, {name}!"
    • ('Hello, ' + name + '!')
    Show correct answer

    Correct answer: `Hello, ${name}!`

    Explanation: Template literals in JavaScript use backticks and the ${} syntax to interpolate variables, as shown in the correct answer. The single and double quote options use incorrect variable notation, and the last option uses string concatenation, not a template literal.

  10. Question 10: Default Parameters

    Which example correctly sets a default value for a function parameter?

    • function greet(name = 'Guest') { return name; }
    • function greet(name : 'Guest') { return name; }
    • function greet(name) { name = 'Guest'; return name; }
    • function greet['name' = 'Guest'] { return name; }
    Show correct answer

    Correct answer: function greet(name = 'Guest') { return name; }

    Explanation: Setting a default value is done by assigning a value during parameter declaration, as in the correct answer. The second option uses a colon, which is not valid syntax. The third option assigns a value inside the function, but this can overwrite any passed value. The fourth option uses an invalid bracket syntax.

  11. Question 11: Falsy Values

    Which of these is a falsy value in JavaScript?

    • 0
    • 1
    • 'false'
    • 'null'
  12. Question 12: Array Methods

    What method adds a new element to the end of an array in JavaScript?

    • push()
    • unshift()
    • pop()
    • shift()
    Show correct answer

    Correct answer: push()

    Explanation: The push() method adds an element to the end of an array. The unshift() method adds to the beginning. The pop() method removes from the end, and shift() removes from the beginning. Only push() performs the described action.

  13. Question 13: Loops

    Which JavaScript loop is best used to iterate through all keys in an object?

    • for...in
    • for...of
    • while
    • do...in
    Show correct answer

    Correct answer: for...in

    Explanation: The for...in loop iterates over the enumerable property keys of an object. The for...of loop is used for iterable objects like arrays, which is different. The standard while loop does not directly iterate through keys. 'do...in' is not a valid JavaScript loop syntax.

  14. Question 14: Closures

    What does a closure in JavaScript allow you to do?

    • Access variables from an outer function even after it finishes execution.
    • Close all open functions automatically.
    • Convert a string to an integer.
    • Define a variable with limited scope.
    Show correct answer

    Correct answer: Access variables from an outer function even after it finishes execution.

    Explanation: A closure enables inner functions to access variables from an outer function's scope even after the outer function has returned. The second option describes no real mechanism. Converting strings to integers is unrelated. The last option partially reflects let scope, not closures.

  15. Question 15: Event Handling

    Which method attaches an event handler to a DOM element in JavaScript?

    • addEventListener()
    • attachEventHandler()
    • setEvent()
    • listenEvent()
    Show correct answer

    Correct answer: addEventListener()

    Explanation: addEventListener() is the correct JavaScript method to attach an event handler to a DOM element. The other options are either outdated or not part of standard JavaScript API. For example, attachEventHandler() and listenEvent() do not exist.

  16. Question 16: Hoisting

    What is variable hoisting in JavaScript?

    • The process where variable declarations are moved to the top of their scope.
    • The act of converting variables to strings automatically.
    • Sorting variable names alphabetically at runtime.
    • Automatically clearing all variables at the end of execution.
    Show correct answer

    Correct answer: The process where variable declarations are moved to the top of their scope.

    Explanation: Hoisting is JavaScript's behavior of moving variable and function declarations to the top of their containing scope during compilation. It has nothing to do with string conversion, variable sorting, or automatic clearance at the end of execution. Only the first option explains the correct behavior.