Fundamentals of JavaScript: Essential Interview Questions Quiz

Enhance your understanding of JavaScript with this beginner-friendly quiz covering key concepts such as data types, hoisting, scope, and core features. Perfect for candidates preparing for JavaScript interviews and learners mastering core JavaScript basics.

  1. JavaScript Data Types

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

    1. Object
    2. Undefined
    3. Number
    4. Boolean

    Explanation: Object is not a primitive data type in JavaScript; it is a reference type. Boolean, Number, and Undefined are all primitive data types. Objects are used to store collections of data, while primitives represent simple values. Choosing Object as a primitive is incorrect because it behaves fundamentally differently from, for instance, Number or Boolean.

  2. Hoisting in JavaScript

    What is the main characteristic of hoisting in JavaScript functions?

    1. Variables declared with const can be accessed before they are initialized
    2. Variables defined with let are always hoisted
    3. Function declarations are moved to the bottom
    4. Function declarations are moved to the top of their scope

    Explanation: In JavaScript, function declarations are hoisted, meaning they are moved to the top of their enclosing scope and can be called before they appear, making code more flexible. Variables declared with let and const are hoisted but not initialized until the code executes, so accessing them early causes an error. Function declarations are not moved to the bottom, and const variables cannot be used before initialization.

  3. The 'debugger' Keyword

    What does the 'debugger' statement do in JavaScript when placed inside code?

    1. Skips the current function
    2. Ignores errors
    3. Stops code execution and opens debugging tools
    4. Deletes a variable

    Explanation: When the 'debugger' statement is reached, JavaScript stops executing and, if a debugger is present, it opens debugging tools at that point. It does not ignore errors, skip functions, or delete variables. The main purpose is to assist developers in checking the state of their application during run time.

  4. Equality Operators

    How does the '===' operator behave differently from the '==' operator in JavaScript?

    1. '===' allows type coercion
    2. '===' is only for strings
    3. '===' compares only values
    4. '===' compares both value and type

    Explanation: The '===' operator checks for strict equality, meaning it compares both value and data type, so '5' === 5 returns false. The '==' operator allows type coercion, making it less strict. '===' does not compare values only, does not allow coercion, and is not limited to strings.

  5. var vs let

    What is a key difference between using the 'var' and 'let' keywords for variable declarations?

    1. 'let' variables are function-scoped
    2. 'let' is block-scoped while 'var' is function-scoped
    3. 'var' supports block scope
    4. 'let' variables can't be reassigned

    Explanation: 'let' allows variables to be limited to the block, statement, or expression, while 'var' is scoped to the entire function. 'var' does not support block scope. 'let' can be reassigned, and it is not function-scoped, so those options are incorrect.

  6. Type Coercion

    Which scenario is an example of implicit type coercion in JavaScript?

    1. Boolean(0)
    2. console.log(2 + '3')
    3. let num = 7
    4. parseInt('5', 10)

    Explanation: In the expression 2 + '3', JavaScript automatically converts the number 2 to a string and concatenates it, producing '23'. parseInt and Boolean are examples of explicit type conversion because the type is changed deliberately using a function. 'let num = 7' does not involve any type change.

  7. Dynamically Typed Language

    Is JavaScript considered a statically typed or dynamically typed language?

    1. Statically typed
    2. Explicitly typed
    3. Strongly typed
    4. Dynamically typed

    Explanation: JavaScript is dynamically typed, meaning variable types are determined at runtime and you can change a variable's type later. It is not statically or explicitly typed, which would require type declarations. Although types matter, JavaScript is not considered strongly typed, as it allows type coercion.

  8. NaN Property

    What does NaN represent in JavaScript?

    1. New Array Name
    2. Null and Nil
    3. Not a Number
    4. Negative and None

    Explanation: NaN stands for 'Not a Number' and indicates that a value is not a legal number. Null and Nil represent absence of any value, not something that fails to be a number. 'Negative and None' and 'New Array Name' are incorrect and unrelated to the term NaN.

  9. Pass by Value vs Reference

    In JavaScript, which type of value is passed by reference when assigned to a function parameter?

    1. Objects
    2. Primitive types
    3. Numbers
    4. Strings

    Explanation: Objects in JavaScript are passed by reference, meaning changes inside the function affect the original object. Primitives like strings and numbers are passed by value, so copies are made and the original is unchanged. Thus, only objects fit as a correct answer here.

  10. Immediately Invoked Function Expression (IIFE)

    Which code snippet shows an Immediately Invoked Function Expression in JavaScript?

    1. (function(){ /* code */ })();
    2. var x = 5;
    3. function test() {}
    4. if (true) {}

    Explanation: An IIFE is a function that is defined and immediately executed, which is done using the (function(){ /* code */ })(); syntax. 'function test() {}' is only a regular function declaration. 'if (true) {}' is a control structure, and 'var x = 5;' is just a variable assignment, so neither of these are IIFEs.