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.
Which of the following is NOT a primitive data type in JavaScript?
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.
What is the main characteristic of hoisting in JavaScript functions?
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.
What does the 'debugger' statement do in JavaScript when placed inside code?
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.
How does the '===' operator behave differently from the '==' operator in JavaScript?
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.
What is a key difference between using the 'var' and 'let' keywords for variable declarations?
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.
Which scenario is an example of implicit type coercion in JavaScript?
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.
Is JavaScript considered a statically typed or dynamically typed language?
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.
What does NaN represent in JavaScript?
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.
In JavaScript, which type of value is passed by reference when assigned to a function parameter?
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.
Which code snippet shows an Immediately Invoked Function Expression in JavaScript?
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.