Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🟣 Javascript interview questions and answers to help you prepare for your next technical interview in 2025.

NotificationsYou must be signed in to change notification settings

Devinterview-io/javascript-interview-questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

web-and-mobile-development

You can also find all 100 answers here 👉Devinterview.io - JavaScript


1. What are thedata types present in JavaScript?

JavaScript hasprimitive andcomposite data types.

Primitive Data Types

  • Boolean: Represents logical values oftrue orfalse.

  • Null: Denotes the lack of a value.

  • Undefined: Indicates a variable that has been declared but has not been assigned a value.

  • Number: Represents numeric values, including integers and floats.

  • BigInt: Allows for representation of integers with arbitrary precision.

  • String: Encapsulates sequences of characters.

  • Symbol (ES6): Provides a unique, immutable value.

Composite Data Types

  • Object: Represents a set of key-value pairs and is used for more complex data structures.
  • Function: A callable object that can be defined using regular function syntax or using thenew Function() constructor (rarely used).

Notable Characteristics

  • JavaScript isdynamically-typed, meaning the data type of a variable can change during the execution of a program.
  • Data type coercion can occur, where values are implicitly converted from one type to another in specific contexts, such as during comparisons.
  • Arithmetic operations, particularly when one of the operands is a string, can lead toimplicit type conversions.

2. What is the difference betweennull andundefined?

While bothnull andundefined represent "no value" in JavaScript, they are distinct in their roles and origins.

Origin and Context

  • null usually denotes an intentionallyabsent value, and developers can set a variable to null to signify the absence of an object or a value. For example, if an API call doesn't return data, you might set a variable to null.

  • undefined typically indicates a variable that has been declared but not yet been assigned a value, or a property that doesn't exist on an object.

Variable Initialization and Assignment

  • Variables that haven't been assigned a value areundefined by default, unless explicitly set tonull.
    letfoo;// undefinedletbar=null;// null

Function Arguments

  • When a function is called, and the parameter isn't provided or its value is not set, the parameter isundefined.
  • null would instead be an explicit value provided as an argument.

Object Properties

  • If you try to access a property on an object that doesn't exist, the result isundefined.

    letobj={};console.log(obj.nonExistentProperty);// undefined
  • Null can be used to clear a property value in an object that was previously set.

    letobj={prop:'value'};obj.prop=null;

The Equality Operation

  • In JavaScript,undefined andnull are treated as equal when using loose equality (==) but not strict equality (===).

Use-Cases and Best Practices

  • When you initialize a variable and are not ready to assign a meaningful value, it's more common to useundefined instead ofnull to indicate that the value isn't there yet.
  • For example, if you declare a user object but don't have their details yet, you might keep it asundefined.

Code Example

Here is the JavaScript code:

letvar1;letvar2=null;letobject={a:1,b:undefined};functiontest(arg1,arg2){console.log(arg1);// undefined: not providedconsole.log(arg2);// null: provided as such}functionclearProperty(prop){deleteobject[prop];}console.log(var1);// undefinedconsole.log(var2);// nullconsole.log(object.a);// 1console.log(object.b);// undefinedconsole.log(object.c);// undefinedtest();// Both arguments are undefinedtest(1,null);// arg1 is 1, arg2 is nullclearProperty('b');// Removes property 'b' from objectconsole.log(object.b);// undefined: Property 'b' was removed, not set to null

3. How does JavaScript handletype coercion?

Type Coercion in JavaScript refers to the automatic conversion of values from one data type to another.

Explicit and Implicit Coercion

  • Explicit: Achieved through methods such asparseInt(),Number(), andtoString().
  • Implicit: Automatically occurs during operations or comparisons. For example, combining a string and a number in an addition results in the automatic conversion of the number to a string.

Common Coercion Scenarios

  1. Arithmetic Operations: Strings are coerced to numbers.

    • Example:"5" - 3 evaluates to2, as the string is coerced to a number.
  2. Loose Equality (==): Data types are often modified for comparisons.

    • Example:"4" == 4 istrue due to string coercion before the comparison.
  3. Conditionals (if and Ternary Operators): Truthiness or falsiness is determined.

    • Example:if(1) evaluates totrue because1 coerces totrue.
  4. Logical Operators: Non-boolean values are coerced to booleans.

    • Example:"hello" && 0 evaluates to0 because the truthy"hello" short-circuits the&& operation, and0 coerces tofalse.

4. Explain the concept ofhoisting in JavaScript.

Hoisting is a JavaScript mechanism that involves moving variable and function declarations to the top of their containing scopeduring the compile phase. However, the assignments to these variables or the definitions of functions remain in place.

For instance, even though the call tomyFunction appears before its definition, hoisting ensures that it doesn't cause an error.

Hoisting in Action

Here's a Code Example:

console.log(myVar);// UndefinedvarmyVar=5;console.log(myVar);// 5// The above code is equivalent to the following during the compile phase:// var myVar;// console.log(myVar);// myVar = 5;console.log(sayHello());// "Hello, World!"functionsayHello(){return"Hello, World!";}// The above code is equivalent to the following during the compile phase:// function sayHello() {//     return "Hello, World!";// }// console.log(sayHello());

Why Hoisting Matters

Understanding hoisting can help you prevent certain unexpected behaviors in your code. For example, it can shed light on unexpected "undefined" values that might appear even after a variable is declared and initialized.

Global Scope and Hoisting

In the global scope, variables declared withvar and functions are always hoisted to the top. For example:

// During the compile phase, the following global declarations are hoisted:// var globalVar;// function globalFunction() {}console.log(globalVar);// Undefinedconsole.log(globalFunction());// "Hello, Global!"varglobalVar="I am global Var!";functionglobalFunction(){return"Hello, Global!";}

Local Scope and Hoisting

Variables and functions declared in local scopes within functions are also hoisted to the top of their scope.

Here's a Code Example:

functionhoistingInLocalScope(){// These local declarations are hoisted during the compile phase:// var localVar;// function localFunction() {}console.log(localVar);// UndefinedlocalVar="I am a local var!";console.log(localFunction());// "Hello, Local!"varlocalVar;functionlocalFunction(){return"Hello, Local!";}}

Best Practices

To write clean, readable code, it's important to:

  • Declare variables at the top of your scripts or functions to avoid hoisting-related pitfalls.
  • Initialize variables before use,regardless of hoisting, to ensure predictable behavior.

ES6 and Hoisting

With the introduction oflet andconst in ES6, JavaScript's behavior has adapted. Variables declared usinglet andconst are still hoisted, but unlikevar, they arenot initialized.

Here's an Example:

console.log(myLetVar);// ReferenceError: Cannot access 'myLetVar' before initializationletmyLetVar=5;

Constants and Hoisting

const andlet behave similarly when hoisted, but their difference lies in the fact thatconst must be assigned a value at the time of declaration, whereaslet does not require an initial value.

Here's an Example:

console.log(myConstVar);// ReferenceError: Cannot access 'myConstVar' before initializationconstmyConstVar=10;console.log(myLetVar);// UndefinedletmyLetVar=5;

5. What is thescope in JavaScript?

Scope defines the accessibility and lifetime of variables in a program. In JavaScript, there are two primary types:Global Scope andLocal Scope.

Global Scope

Any variable declaredoutside of a function is in the global scope. These can be accessed from both within functions and from other script tags.

Example: Global Scope

Here is the JavaScript code:

letglobalVar='I am global';functiontestScope(){console.log(globalVar);// Output: 'I am global'}testScope();console.log(globalVar);// Output: 'I am global'

Local Scope

Variables declared within afunction (usinglet orconst or prior to JavaScript ES6 withvar) have local scope, meaning they are only accessible within that function.

Example: Local Scope

Here is the JavaScript code:

functiontestScope(){letlocalVar='I am local';console.log(localVar);// Output: 'I am local'}// This statement will throw an error because localVar is not defined outside the function scope// console.log(localVar);

Block Scope

Starting fromES6, JavaScript also supports block scope, where variables defined inside code blocks (denoted by{} such as loops or conditional statements) usinglet orconst are accessible only within that block.

Example: Block Scope

Here is the JavaScript code:

functiontestScope(){letlocalVar='I am local';if(true){letblockVar='I am local to this block';console.log(localVar,blockVar);// Both will be accessible}// This statement will throw an error because blockVar is not defined outside the block scope// console.log(blockVar);}testScope();

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

Strict equality (===) in JavaScript requires both value and type to match, testing for more specific conditions and reducing the likelihood of unexpected results.

In contrast, theabstract equality comparison (==) can lead to type coercion, potentially causing counterintuitive outcomes.

While both comparison modes test value equality,=== ensures an additional match of data type.

Illustrative Example: Abstract vs. Strict Equality

  • Abstract Equality:
    • 5 == '5' evaluates totrue because JavaScript converts the string to a number for comparison.
  • Strict Equality:
    • 5 === '5' evaluates tofalse because the types are not the same.

Key Considerations

  • Type Safety:=== is safer as it avoids unwanted type conversions.
  • Performance:=== can be faster, especially for simple comparisons, as it doesn't involve type coercion or additional checks.
  • Clarity: Favoring=== can make your code clearer and more predictable.

Common Best Practices

  • Use Strict Equality by Default: This approach minimizes unintended side effects.
  • Consider Type Coercion Carefully: In specific cases or with proven understanding,== can be suitable, but be cautious about potential confusion.

Code Example: Equality Operators

Here is the JavaScript code:

// Abstract equalityconsole.log('5'==5);// trueconsole.log(null==undefined);// trueconsole.log(0==false);// true// Strict equalityconsole.log('5'===5);// falseconsole.log(null===undefined);// falseconsole.log(0===false);// false

7. Describeclosure in JavaScript. Can you give an example?

In JavaScript,closures enable afunction to access its outer scope, retaining this access even after the parent function has finished executing. This mechanism provides a powerful tool for data encapsulation and privacy.

Core Concept

When afunction is defined within another function, it maintains a reference to the variables from the outer function, even after the outer function has completed execution and its local variables are typically no longer accessible.

Key Components

  1. Outer Function (Parent function): It contains the inner functions or closures.
  2. Inner Function (Closure): Defined within the parent function, it references variables from the outer function.
  3. Lexical Environment: The context where the inner function is defined, encapsulating the scope it has access to.

Example: Password Generator

Consider a simple scenario of a function in charge of generating a secret password:

  1. The outer function,generatePassword, defines a local variable,password and returns an inner functiongetPassword.
  2. The inner function,getPassword, has exclusive access to thepassword variable even aftergeneratePassword has executed.

Here is the JavaScript code:

functiongeneratePassword(){letpassword='';constcharacters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';constpasswordLength=8;for(leti=0;i<passwordLength;i++){password+=characters.charAt(Math.floor(Math.random()*characters.length));}returnfunctiongetPassword(){returnpassword;};}constgetPassword=generatePassword();console.log(getPassword());// Outputs the generated password.

In this example,getPassword still has access to thepassword variable after thegeneratePassword function has completed, thanks to the closure mechanism.

Application

  • Data Privacy: JavaScript design patterns like the Module and Revealing Module Patterns use closures to keep data private.

  • Timeouts and Event Handlers: Closures help preserve the surrounding context in asynchronous operations such assetTimeout and event handlers.

Pitfalls to Avoid

  • Memory Leakage: If not used carefully, closures can cause memory leaks, as the outer function's variables continue to live in memory because of the closure link.
  • Stale Data: Be mindful of shared variables that might change after a closure has been defined, leading to unexpected behavior.

Browser Compatibility

The concept of closures is a fundamental aspect of the JavaScript language and is supported by all modern browsers and environments.

8. What is the 'this keyword' and how does its context change?

In JavaScript, the context ofthis refers to the execution context, typically an object that owns the function wherethis is used.

'this' in the Global Scope

Innon-strict mode,this in the global scope refers to thewindow object. Instrict mode,this isundefined.

'this' in Functions

Innon-arrow functions, the value ofthis depends on how the function isinvoked. When invoked:

  • As a method of an object:this is the object.
  • Alone: In a browser,this iswindow orglobal in Node.js. In strict mode, it'sundefined.
  • Withcall,apply, orbind:this is explicitly set.
  • As a constructor (withnew):this is the newly created object.

'this' in Arrow Functions

Arrow functions have afixed context forthis defined atfunction creation and are not changed by how they are invoked.

  • They donot have their ownthis.
  • They use thethis from their surrounding lexical context (the enclosing function or global context).

Code Example: Global Context

Here is the JavaScript code:

// MainletglobalVar=10;functionglobalFunction(){console.log('Global this: ',this.globalVar);console.log('Global this in strict mode: ',this);}globalFunction();// Output: 10, window or undefined (in strict mode)// In Node.js, it will be different, because "window" is not defined. But "this" will refer to the global object.

9. What arearrow functions and how do they differ from regular functions?

Let's look at the key features ofarrow functions and how they differ from traditional functions in JavaScript.

Arrow Functions: Key Features

  • Concise Syntax:

    • Especially useful for short, one-liner functions.
    • No need forfunction keyword orbraces if there's a single expression.
  • Implicit Return:

    • When there's no explicit{ return ... ;} statement, arrow functions return the result of the single expression inside.
  • this Binding:

    • Does not have itsownthis. It's "inherited" from the surrounding (lexical) context. This feature is known as 'lexical scoping'.

Code Example: Standard Function vs. Arrow Function

Here is the JavaScript code:

// Standard Functionfunctiongreet(name){return"Hello, "+name+"!";}// Arrow FunctionconstgreetArrow=name=>"Hello, "+name+"!";

In the code above,greet is a standard function, whilegreetArrow is an arrow function, showcasing the difference in syntax and required keywords.

When to Use Arrow Functions

  • Event Handlers: Ideal for concise, inline event handling, wherethis context can be inherited from the lexical scope.

  • Callback Functions: Useful for array methods likemap,filter, andreduce.

  • Avoidance ofthis Redefinition: When you want to maintain the surrounding context ofthis and avoid unintended redefinition.

Code Example: Arrow Function andthis Context

Here is the JavaScript code:

// Using traditional functionsdocument.getElementById('myButton').onclick=function(){console.log('Button clicked:',this);// Refers to the button element};// Using arrow functionsdocument.getElementById('myButton').onclick=()=>{console.log('Button clicked:',this);// Refers to the global/window object};

In the arrow function example, the context ofthis does not refer to the button element, but to the globalwindow object, because arrow functions do not have their own binding ofthis. Instead, they inheritthis from their lexical scope, which in this case is the global context.

10. What aretemplate literals in JavaScript?

Template literals are a feature in modern JavaScript versions that offer a more flexible and readable way to work with strings. They are often referred to as "template strings".

Key Features

  • Multiline Text: Template literals support multiline strings without requiring escape characters or string concatenation with a+ sign.
  • String Interpolation: They enable the seamless embedding of JavaScript expressions within strings, using${}.

Syntax

  • Single Versus Double Quotes: For template literals, use backticks (`) instead of single quotes ('') or double quotes ("").
  • Placeholder: The${expression} placeholder within the backticks allows for variable and expression injection.

Example:

letname="John";letmessage=`Hi${name}!`;console.log(message);// Output: "Hi John!"

Benefits

  • Readability: They can make code more understandable, especially when dealing with longer or complex strings, by keeping content closer to its intention.
  • Interpolation & Expression: Template literals reduce verbosity and rendering logic when integrating dynamic data.

Code Example: Multiline Text and String Interpolation

// Regular Stringletpoem="Roses are red,\nViolets are blue,\nSugar is sweet,\nAnd so are you.";// Template LiteralletpoemTemplate=`  Roses are red,  Violets are blue,  Sugar is sweet,  And so are you.`;

Browser Compatibility Concerns

Template literals are universally supported in modern browsers and are now considered acore JavaScript feature. However, they may not work in older browsers such as Internet Explorer without transpilation or polyfilling.

11. What is ahigher-order function in JavaScript?

Ahigher-order function in JavaScript is a function that can take other functions as arguments or can return functions. This feature enables functional programming paradigms such asmap,reduce, andfilter. Higher-order functions offer versatility and modularity, fostering streamlined, efficient code.

Key Characteristics

  • First-class functions: Functions in JavaScript are considered first-class, meaning they are a legitimate data type and can be treated like any other value, including being assigned to variables, stored in data structures, or returned from other functions.

  • Closure support: Due to closures, a higher-order function can transport not just the enclosed data within the function definition, but also the lexical environment in which that data resides.

  • Dynamic code: Because JavaScript allows functions to be dynamically constructed and named, they can be dynamically passed to higher-order functions.

Practical Applications

  • Callback Execution: Functions likesetTimeout andaddEventListener take a function as an argument and are thus higher-order.

  • Event Handling: Many event-driven systems leverage higher-order functions for tasks such as event subscription and emission.

  • Iterative Operations: Themap,filter, andreduce functions in JavaScript operate on arrays and require functions to be passed, making them higher-order.

  • Code Abstraction: Higher-order functions enable the encapsulation of repetitive tasks, promoting cleaner, more readable code.

Code Example: Higher-order Functions

Here is the JavaScript code:

// Simple higher-order functionfunctionmultiplier(factor){returnfunction(num){returnnum*factor;};}// Invoke a higher-order functionconsttwice=multiplier(2);console.log(twice(5));// Output: 10// Functional programming with higher-order functionsconstnumbers=[1,2,3,4,5];constdoubled=numbers.map(multiplier(2));// [2, 4, 6, 8, 10]consttripled=numbers.map(multiplier(3));// [3, 6, 9, 12, 15]

12. Can functions be assigned as values to variables in JavaScript?

Yes,JavaScript supports first-class functions, meaningfunctions can be treated as variables and then assigned to other variables or passed as arguments to other functions.

Functions defined as regular functions or arrow functions are both first-class in JavaScript.

Practical Code Example

Here is the JavaScript code:

// Define a functionfunctiongreet(){console.log('Hello!');}// Assign the function to a variableletsayHello=greet;// Call the function through the variablesayHello();// Output: "Hello!"// Reassign the variable to a new functionsayHello=function(){console.log('Bonjour!');};// Call it again to see the new behaviorsayHello();// Output: "Bonjour!"

Practical Use Cases

  • Callbacks: Functions can be passed as parameters to other functions.

  • Event Handling: In web development, functions define how to respond to specific events, and these functions are often attached to event listeners.

  • Modular Development: In programming patterns like the Module pattern, functions are defined within a scope and then returned, similar to variables.

  • Higher-Order Functions: These functions operate on other functions, taking them as arguments or returning them, and are an essential part of many modern JavaScript libraries and frameworks.


13. How dofunctional programming concepts apply in JavaScript?

Functional Programming (FP) concepts in JavaScript are a direct result of the language's first-class functions. Key FP principles, such as immutability, pure functions, anddeclarative style, play a crucial role.

Core Concepts

First-Class Functions and Higher-Order Functions

JavaScript treats functions as first-class citizens, allowing them to be assigned to variables, passed as parameters, and returned from other functions. This feature is foundational to FP in the language.

Code Example:

Here is the JavaScript code:

constsayHello=()=>console.log('Hello!');construnFunction=(func)=>func();runFunction(sayHello);// Output: "Hello!"

14. What areIIFEs (Immediately Invoked Function Expressions)?

TheImmediately Invoked Function Expression (IIFE) design pattern employs an anonymous function that gets executed promptly after its definition.

Key characteristics of IIFEs include localized variable scopes and immediate activation upon interpreter parsing.

Code Example: IIFE

Here is the JavaScript code:

(function(){varfoo='bar';console.log(foo);})();

In this example, the function is enclosed within parentheses, ensuring the enclosed function is evaluated as an expression. Subsequently, it is invoked with a trailing pair of parentheses.

Core Functions of IIFE

  1. Encapsulation: Through lexical scoping, IIFEs safeguard variables from leaking into the global scope. This, in turn, averts unintended variable tampering in the global context.

  2. Data Hiding: Internal functions or data can be hidden from external access, providing a mechanism for information concealment and access control.

  3. Initialization: The IIFE structure is ideal for setting up initial conditions, like binding events or pre-processing data.

Use Cases

  • Avoiding Variable Pollution: When interfacing with libraries or inserting code snippets, IIFEs prevent global scope pollution.

  • Module Patterns: IIFEs, in combination withclosures, lay the groundwork for modular code organization by shielding private variables and functions.

Modern Alternatives

With the introduction of ES6 and itslet andconst declarations, as well as block-scoped lexical environments, the necessity of IIFEs has reduced. Additionally,arrow functions provide a more concise method for defining immediately invoked functions.

IIFE Variants

  1. Parentheses Invocation: A pair of parentheses immediately invoke the enclosed function. While this approach is more extensive, it's devoid of self-documenting advantages.

    (function(){console.log('Invoked!');})();
  2. Wrapping in Operators: Similar to using parentheses for invocation, the!,+, or- operators are sometimes used for invoking clarity. For instance:

    !function(){console.log('Invoked!');}();
  3. Named IIFE: Though not as common, naming an IIFE can assist with self-referencing. This is most effective when the intention is to have a more comprehensive stack trace during debugging.

    (functionfactorial(n){if(n<=1)return1;returnn*factorial(n-1);})(5);

Caution on Minification

When leveraging IIFEs, exercise caution while using minifiers to shrink JavaScript files. Minification might lead to unintended outcomes, altering the previous scope expectations.

15. How do you createprivate variables in JavaScript?

In JavaScript, encapsulating private state within an object can be achieved using aclosure. This ensures the state is local to the object and not directly accessible from outside.

How Closures Work

Aclosure allows a function to retain access to thelexical environment (the set of variable bindings at the point of function declaration) in which it was defined, even when the function is executed outside that lexical environment.

This means that anyinner function, defined inside another function, has access to theouter function's variables, and that access is maintained even after the outer function has finished executing.

For example:

functionouterFunction(){letouterVar='I am outer';// This variable is in the lexical environment of outerFunctionfunctioninnerFunction(){console.log(outerVar);// Accesses outerVar from the lexical environment of outerFunction}returninnerFunction;}letmyInnerFunction=outerFunction();myInnerFunction();// Logs: "I am outer"

Here,innerFunction retains access toouterVar.

Practical Implementation with Constructor Functions and Modules

Constructor Functions

When defining a JavaScriptconstructor function withfunction andnew, closure can be used to associate private state with each instance:

functionGadget(){letsecret='top secret';this.setSecret=function(value){secret=value;};this.getSecret=function(){returnsecret;};}letphone=newGadget();phone.setSecret('new secret');console.log(phone.getSecret());// 'new secret'

In this example,secret is private to eachGadget instance, thanks to closure.

Modules

In modern JavaScript,module patterns combined withimmediately-invoked function expressions (IIFE) are often used for encapsulation and data hiding.

  • Therevealing module pattern enables selective exposure of private members.

  • TheIIFE pattern immediately executes and returns the object to be assigned, effectively creating a module.

Here is the code:

letmyModule=(function(){letprivateVariable='I am private';functionprivateMethod(){console.log('I am a private method');}return{publicMethod:function(){console.log('I am a public method');},getPrivateVariable:function(){returnprivateVariable;}};})();console.log(myModule.getPrivateVariable());// 'I am private'myModule.privateMethod();// Throws an error because privateMethod is not exposed

In this example,privateVariable andprivateMethod are accessible only within the IIFE's lexical environment, thus making them private.

JavaScript tools like TypeScript and Babel also offer modules such asmodule.export, providing additional options for encapsulation.

Explore all 100 answers here 👉Devinterview.io - JavaScript


web-and-mobile-development


[8]ページ先頭

©2009-2025 Movatter.jp