JavaScript Interview Fundamentals
Shared by a Flashcards.gg user · generated with AI · Report this set
Core JavaScript concepts and fundamentals for junior developer technical interviews.
Tap the card to flip it
Cards · 51
- What are the seven primitive data types in JavaScript?
- Primitives are immutable values: string, number, bigint, boolean, undefined, symbol, and null.
- What is the difference between primitive types and reference types?
- Primitives are stored directly by value on the stack, while objects are stored in the heap and accessed by memory reference.
- Why does typeof null return 'object'?
- It is a legacy bug from JS's initial version where type tags allocated zero to objects, which matched null's null-pointer representation.
- What is the difference between undefined and null?
- Undefined means a variable has been declared but not assigned a value; null represents an intentional absence of any object value.
- What is NaN in JavaScript?
- NaN stands for Not-a-Number, a numeric data type representing an undefined or unrepresentable computational result.
- How do you reliably check if a value is NaN?
- Use Number.isNaN(value), because NaN is not equal to anything, including itself, making standard equality checks fail.
- What is implicit type coercion?
- The automatic conversion of values from one data type to another by the JS engine during operations like string concatenation or comparison.
- What is explicit type conversion (type casting)?
- The deliberate conversion of a value from one type to another using built-in functions like Number(), String(), or Boolean().
- What is the difference between == and ===?
- The loose equality operator (==) performs type coercion before comparison, whereas strict equality (===) checks both value and type.
- What happens when using the '+' operator between a number and a string?
- The number is implicitly coerced into a string, and string concatenation occurs instead of arithmetic addition.
- What happens when using the '-' operator between a string and a number?
- The string is implicitly coerced into a numeric value, and numeric subtraction is performed.
- What are falsy values in JavaScript?
- Values that evaluate to false in boolean contexts: false, 0, -0, 0n, empty string, null, undefined, and NaN.
- What is the result of [] + [] and why?
- An empty string; both arrays are coerced into primitive strings via their toString() methods and concatenated.
- What is the result of [] + {} and why?
- The string '[object Object]', because the array becomes an empty string and the object converts to its string representation.
- What is execution context?
- An internal environment where JS code is evaluated and executed, containing the variable environment, scope chain, and this value.
- What are the phases of the JavaScript execution context?
- The creation phase, where memory is allocated for variables and functions, and the execution phase, where code is run line by line.
- What is the call stack?
- A LIFO (Last In, First Out) data structure that keeps track of the currently running execution contexts in the JavaScript engine.
- What is lexical scope?
- A variable scoping mechanism where accessibility is determined strictly by the physical location of variables within the written source code.
- What is global scope?
- The outermost scope; variables declared here are accessible anywhere in the program and attached to the global object in non-strict mode.
- What is function scope?
- Variables declared with var inside a function are accessible only within that function and nested functions, not from outer scopes.
- What is block scope?
- Variables declared with let and const are limited in scope to the specific curly-bracket block ({}) in which they are defined.
- What is the scope chain?
- The mechanism JavaScript uses to resolve variable references by traversing outward from the local scope through parent scopes up to global.
- What is variable shadowing?
- When a variable declared in an inner scope has the exact same name as a variable in an outer scope, masking access to the outer one.
- What is hoisting?
- The process during the creation phase where variable and function declarations are allocated memory before the code executes.
- How are function declarations hoisted compared to function expressions?
- Function declarations are fully hoisted with their implementation; function expressions assigned to variables only hoist the variable name.
- How does hoisting behave for var variables?
- Variables declared with var are hoisted and initialized to undefined immediately during the creation phase.
- How does hoisting behave for let and const variables?
- They are hoisted to the top of their block but remain uninitialized in memory, leading to a ReferenceError if accessed early.
- What is the Temporal Dead Zone (TDZ)?
- The period between entering a scope and declaring a let or const variable, during which accessing the variable throws a ReferenceError.
- What is a closure in JavaScript?
- A function bundled together with references to its surrounding lexical environment, allowing it to access outer variables after outer execution finishes.
- How does the JavaScript engine keep closed-over variables in memory?
- Variables referenced by inner closures are allocated on the heap rather than the stack, preventing garbage collection.
- What is a common practical use case for closures?
- Data privacy and encapsulation, such as creating private variables and factory functions with protected internal state.
- What problem occurred with var inside a for-loop with setTimeout?
- Because var is function-scoped, every iteration shared the same variable reference, which held the final incremented value when the timer fired.
- How does replacing var with let fix the for-loop closure issue?
- The let keyword creates a new, distinct lexical binding for each loop iteration, preserving the current index for each closure.
- What is the 'this' keyword in JavaScript?
- A reference to the object that represents the context in which the current function was invoked.
- How is 'this' determined in default function invocation (non-strict)?
- In non-strict mode, calling a plain standalone function binds 'this' to the global object (window or global).
- How is 'this' determined in default function invocation under strict mode?
- Under strict mode, calling a standalone function leaves 'this' as undefined instead of defaulting to the global object.
- What does 'this' point to in implicit method binding?
- When a function is invoked as a method of an object (obj.method()), 'this' points directly to the object preceding the dot.
- What is explicit binding with call()?
- A method that invokes a function immediately, explicitly setting 'this' to a provided object and passing arguments individually.
- What is explicit binding with apply()?
- A method that invokes a function immediately, setting 'this' explicitly, but accepts arguments as an array.
- What is explicit binding with bind()?
- A method that returns a brand-new function with a permanently bound 'this' context and optional pre-set arguments.
- How does 'this' behave in arrow functions?
- Arrow functions do not possess their own 'this'; they lexically capture the 'this' value of their immediate enclosing execution context.
- Can you rebind 'this' on an arrow function using bind, call, or apply?
- No; arrow functions disregard explicit context rebinding because their 'this' is strictly lexical.
- What does 'this' point to inside a constructor function called with new?
- The 'new' keyword binds 'this' to the newly created, empty object instance being constructed.
- What are the four steps performed by the 'new' keyword?
- It creates a blank object, sets its prototype chain, binds 'this' to the object, and returns the object (unless a non-primitive is returned).
- What is prototypal inheritance?
- A language feature where objects inherit properties and methods directly from other objects via an internal prototype link.
- What is the difference between prototype and __proto__?
- Function.prototype is the blueprint object used to instantiate new objects; __proto__ is an accessor on instances linking to that blueprint.
- What is the prototype chain?
- A series of linked objects traversed by the runtime to find properties; if missing locally, the engine looks upward until reaching null.
- What sits at the very end of the prototype chain?
- Object.prototype.__proto__, which points directly to null.
- What does Object.create() do?
- It creates a brand new object with its internal prototype (__proto__) explicitly set to the specified object.
- What is the difference between hasOwnProperty() and the 'in' operator?
- hasOwnProperty checks only the object's direct keys, while the 'in' operator checks both direct and inherited prototype keys.
- How do ES6 classes relate to JavaScript prototypes?
- ES6 classes are syntactic sugar over standard prototypal inheritance, using constructor functions and prototypes under the hood.