冚家铲是什么意思| 沙砾是什么意思| 觉是什么结构| 肛裂挂号挂什么科| 雄性激素过高是什么原因| 甲沟炎看什么科室| 肠瘘是什么意思| 梦见大火烧房子是什么意思| 7月29号是什么日子| 羊水多了对宝宝有什么影响| 戒指戴无名指是什么意思| hcg翻倍不好是什么原因造成的| 压箱钱是什么意思| 洗衣机什么品牌好| 瘘是什么意思| 牙疼吃什么食物| 打冷是什么意思| 关节外科主要看什么| 蟑螂屎长什么样| 酒不能和什么一起吃| 胆囊切除后需要注意什么| 1月12日是什么星座| 老婆的妹妹叫什么| 梦见跟别人打架是什么意思| 耳钉后面的塞子叫什么| 十的偏旁有什么字| 3m是什么意思| 1920年属什么生肖| 疏肝理气喝什么茶| 吐血拉血是什么病的症状| 刘邦为什么要杀韩信| 乌托邦是什么意思| rbc是什么意思医学| 甲状腺穿刺是什么意思| 什么原因引起耳石症| 心尖尖是什么意思| 清一色是什么意思| 平均红细胞体积偏高是什么原因| 狗尾巴草有什么功效| 小姨子是什么关系| 读书心得是什么意思| erke是什么牌子| 平均红细胞体积偏高是什么原因| 口胃读什么| 薇诺娜适合什么年龄| 喝什么可以排便通畅| 贫血吃什么补血效果最好| 立春是什么生肖| 为什么女生| 梦见摘枣吃枣是什么意思| 戊午五行属什么| 平均血红蛋白浓度偏低是什么意思| 灵芝有什么功效和作用| 脾肾阳虚是什么意思| 决定的近义词是什么| 1958年是什么年| 93年是什么命| 什么饮料解酒| 三秋是什么意思| 颈椎引起的头晕是什么症状| 代谢是什么| 总是困是什么原因| 经常流鼻血是什么病| 半夜腿抽筋是什么原因| 昔人是什么意思| 名创优品是卖什么的| 月子里生气有什么危害| 鱼为什么睁着眼睛睡觉| 梦见很多坟墓是什么意思| 6.30是什么星座| Mo什么元素| 笑气是什么气体| 计划生育什么时候开始的| 房性期前收缩是什么意思| 吃什么能减脂肪肝| 脂肪肝适合吃什么食物| 急性胰腺炎吃什么药| 诟病是什么意思| 胆囊结石不宜吃什么| 做梦梦到蟒蛇是什么征兆| 什么伐桂| 龙筋是什么| 菊花茶为什么会变绿色| 交公粮是什么意思| 测心率手表什么牌子好| 炖肉放什么容易烂| 糜烂性胃炎有什么症状| 血压压差小是什么原因| 胃寒吃什么食物好| 作是什么意思| 众所周知是什么生肖| 为什么体检要空腹| 什么品牌补水效果好| 什么的童话| 头晕吃什么药好| 大便出血吃什么药好得快| 肺部ct能查出什么病| 包茎是什么意思| 猫一般吃什么| 血常规一般查什么病| 诗五行属性是什么| 汉武帝是什么朝代| 介入科主要看什么病| jhs空调是什么牌子| 43岁属什么生肖| 蛋黄吃多了有什么坏处| 手腕凸起的骨头叫什么| 神经性头疼是什么症状| 移徒是什么意思| 什么是肾结石| 喉咙痛吃什么药好得快| 右手有痣代表什么| 秋千为什么叫秋千| 冰丝纤维是什么面料| p.a.是什么意思| 大姨妈是什么意思| 痛风吃什么药止痛最快| 太平天国失败的根本原因是什么| 晚上脚抽筋是什么原因| 流汗多是什么原因| 健康是什么意思| 三和大神什么意思| 蔚姓氏读什么| 狗尾巴草的花语是什么| taco是什么| 人老放屁是什么原因| 眼白发黄是什么原因| 海笋是什么东西| 回头鱼是什么鱼| 女性吃什么降低雄激素| 来大姨妈量少是什么原因| 唾液酸苷酶阳性是什么意思| 4090是什么意思| 推辞是什么意思| 单飞什么意思| 06是什么生肖| 电解液是什么| cea升高是什么意思| 床垫什么材质的好| 宝宝入盆有什么感觉| 什么品牌奶粉最好| 孩子高低肩有什么好办法纠正| 8月24是什么星座| 山茱萸是什么| 体毛旺盛是什么原因| 湿疹是什么症状图片| 王字旁一个玉读什么| 精神障碍是什么病| 人生轨迹是什么意思| 肚子疼是什么原因| 甘油三酯高什么原因| 五黄煞是什么意思| 一天什么时候血压最高| 姚明什么时候退役的| 脑供血不足吃点什么药| 打嗝不停是什么原因| 女累读什么| 小孩发育迟缓是什么原因造成的| 最好的大学是什么大学| 途字五行属什么| 吃鹅蛋对孕妇有什么好处| 暗示是什么意思| 疱疹是什么原因引起| 兵戎相见是什么意思| 油菜花什么颜色| 海马有什么功效作用| 为什么会有霉菌性阴道炎| 高密度脂蛋白胆固醇偏低是什么意思| 什么门什么户| 看阴茎挂什么科| 非油炸是什么意思| 吃什么水果补铁| 十九岁属什么| 心火旺吃什么| 早搏是什么原因引起的| 一什么鼓| 插管是什么意思| 直肠给药对小孩身体有什么影响| 八朵玫瑰花代表什么意思| 同工同酬是什么意思| 什么什么的天空| 叙字五行属什么| 星字属于五行属什么| 痔疮手术后可以吃什么水果| est.是什么意思| 乔迁之喜送什么花| 想吃辣是身体缺乏什么| 生理是什么意思| 婴儿拉奶瓣是什么原因| 肝气不舒吃什么中成药| 小孩吃牛肉有什么好处| 胃胀气吃什么药| 乳腺瘤不能吃什么| 理想是什么意思| 肾结石都有什么症状| 阴虚内热吃什么药| 什么是鸡头米| 河粉是什么| 展开的近义词是什么| 吃什么减肥最好最快| 绞肠痧是什么病| 药食同源什么意思| 记忆是什么| 农历9月11日是什么星座| 女孩学什么专业好| 无后为大的前一句是什么| 鼻窦炎长什么样图片| 璋字五行属什么| 哈乐是什么药| 211属于什么大学| 什么的黎明| 网球肘是什么症状| 孕吐是什么原因造成的| 产后什么时候来月经正常| 什么牌子的冰箱最好| 慢热型是什么意思| 大便出血吃什么药好得快| 有胆结石的人不能吃什么东西| 拔完智齿后需要注意什么| 出淤泥而不染是什么意思| 属鼠的和什么属相最配| o型血为什么叫贵族血| 月经总是提前是什么原因| 脱轨是什么意思| 鸡属于什么动物| 嗓子痛吃什么消炎药| 财务是什么意思| 10月28日是什么日子| 什么瓜不能吃脑筋急转弯| 什么样的人| 手抖是什么情况| 宫腔内无回声区是什么意思| 爱马仕配货是什么意思| ip地址是什么意思| 丝瓜不能和什么一起吃| 什么嫩芽| 什么是回迁房| 老年人生日送什么礼物| 夜叉是什么| 月经推迟十天是什么原因| 胆囊炎有什么症状| 身心疲惫是什么意思| 甲醛对人体有什么危害| cy什么意思| 窦骁父母是干什么的| feat什么意思| 胃胀气有什么症状| 办护照需要什么证件| 花卉是什么意思| hmb是什么意思| 呃逆什么意思| 六月六是什么节| 味甘是什么意思| 姨妈没来是什么原因| 侄子是什么关系| 拔牙什么时候拔最好| 结婚50年是什么婚| 海鸥吃什么食物| 胃胀不消化吃什么药| 旧历是什么意思| 为什么说成也萧何败也萧何| 再接再励是什么意思| 铊是什么东西| 什么病才吃阿昔洛韦片| 百度
Skip to content

Devinterview-io/javascript-interview-questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

?

History

4 Commits
?
?

Repository files navigation

100 Common JavaScript Interview Questions in 2025

web-and-mobile-development

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


1. What are the data types present in JavaScript?

JavaScript has primitive and composite data types.

Primitive Data Types

  • Boolean: Represents logical values of true or false.

  • 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 the new Function() constructor (rarely used).

Notable Characteristics

  • JavaScript is dynamically-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 to implicit type conversions.

2. What is the difference between null and undefined?

While both null and undefined represent "no value" in JavaScript, they are distinct in their roles and origins.

Origin and Context

  • null usually denotes an intentionally absent 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 are undefined by default, unless explicitly set to null.
    let foo; // undefined
    let bar = null; // null

Function Arguments

  • When a function is called, and the parameter isn't provided or its value is not set, the parameter is undefined.
  • 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 is undefined.

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

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

The Equality Operation

  • In JavaScript, undefined and null 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 use undefined instead of null 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 as undefined.

Code Example

Here is the JavaScript code:

let var1;
let var2 = null;

let object = {
  a: 1,
  b: undefined
};

function test(arg1, arg2) {
  console.log(arg1);  // undefined: not provided
  console.log(arg2);  // null: provided as such
}

function clearProperty(prop) {
  delete object[prop];
}

console.log(var1);     // undefined
console.log(var2);     // null
console.log(object.a); // 1
console.log(object.b); // undefined
console.log(object.c); // undefined

test();               // Both arguments are undefined
test(1, null);        // arg1 is 1, arg2 is null

clearProperty('b');  // Removes property 'b' from object
console.log(object.b); // undefined: Property 'b' was removed, not set to null

3. How does JavaScript handle type 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 as parseInt(), Number(), and toString().
  • 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 to 2, as the string is coerced to a number.
  2. Loose Equality (==): Data types are often modified for comparisons.

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

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

    • Example: "hello" && 0 evaluates to 0 because the truthy "hello" short-circuits the && operation, and 0 coerces to false.

4. Explain the concept of hoisting in JavaScript.

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

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

Hoisting in Action

Here's a Code Example:

console.log(myVar); // Undefined
var myVar = 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!"
function sayHello() {
    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 with var 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); // Undefined
console.log(globalFunction()); // "Hello, Global!"
var globalVar = "I am global Var!";
function globalFunction() {
    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:

function hoistingInLocalScope() {
    // These local declarations are hoisted during the compile phase:
    // var localVar;
    // function localFunction() {}

    console.log(localVar); // Undefined
    localVar = "I am a local var!";
    console.log(localFunction()); // "Hello, Local!"

    var localVar;
    function localFunction() {
        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 of let and const in ES6, JavaScript's behavior has adapted. Variables declared using let and const are still hoisted, but unlike var, they are not initialized.

Here's an Example:

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

Constants and Hoisting

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

Here's an Example:

console.log(myConstVar); // ReferenceError: Cannot access 'myConstVar' before initialization
const myConstVar = 10;

console.log(myLetVar); // Undefined
let myLetVar = 5;

5. What is the scope in JavaScript?

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

Global Scope

Any variable declared outside 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:

let globalVar = 'I am global';

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

testScope();
console.log(globalVar); // Output: 'I am global'

Local Scope

Variables declared within a function (using let or const or prior to JavaScript ES6 with var) have local scope, meaning they are only accessible within that function.

Example: Local Scope

Here is the JavaScript code:

function testScope() {
    let localVar = '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 from ES6, JavaScript also supports block scope, where variables defined inside code blocks (denoted by {} such as loops or conditional statements) using let or const are accessible only within that block.

Example: Block Scope

Here is the JavaScript code:

function testScope() {
    let localVar = 'I am local';
    if (true) {
        let blockVar = '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, the abstract 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 to true because JavaScript converts the string to a number for comparison.
  • Strict Equality:
    • 5 === '5' evaluates to false 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 equality
console.log('5' == 5);      // true
console.log(null == undefined);  // true
console.log(0 == false);    // true

// Strict equality
console.log('5' === 5);     // false
console.log(null === undefined); // false
console.log(0 === false);   // false

7. Describe closure in JavaScript. Can you give an example?

In JavaScript, closures enable a function 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 a function 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 function getPassword.
  2. The inner function, getPassword, has exclusive access to the password variable even after generatePassword has executed.

Here is the JavaScript code:

function generatePassword() {
  let password = '';
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  const passwordLength = 8;
  for(let i = 0; i < passwordLength; i++) {
    password += characters.charAt(Math.floor(Math.random() * characters.length));
  }

  return function getPassword() {
      return password;
  };
}

const getPassword = generatePassword();

console.log(getPassword()); // Outputs the generated password.

In this example, getPassword still has access to the password variable after the generatePassword 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 as setTimeout 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 of this refers to the execution context, typically an object that owns the function where this is used.

'this' in the Global Scope

In non-strict mode, this in the global scope refers to the window object. In strict mode, this is undefined.

'this' in Functions

In non-arrow functions, the value of this depends on how the function is invoked. When invoked:

  • As a method of an object: this is the object.
  • Alone: In a browser, this is window or global in Node.js. In strict mode, it's undefined.
  • With call, apply, or bind: this is explicitly set.
  • As a constructor (with new): this is the newly created object.

'this' in Arrow Functions

Arrow functions have a fixed context for this defined at function creation and are not changed by how they are invoked.

  • They do not have their own this.
  • They use the this from their surrounding lexical context (the enclosing function or global context).

Code Example: Global Context

Here is the JavaScript code:

// Main
let globalVar = 10;

function globalFunction() {
    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 are arrow functions and how do they differ from regular functions?

Let's look at the key features of arrow 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 for function keyword or braces 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 its own this. 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 Function
function greet(name) {
  return "Hello, " + name + "!";
}

// Arrow Function
const greetArrow = name => "Hello, " + name + "!";

In the code above, greet is a standard function, while greetArrow 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, where this context can be inherited from the lexical scope.

  • Callback Functions: Useful for array methods like map, filter, and reduce.

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

Code Example: Arrow Function and this Context

Here is the JavaScript code:

// Using traditional functions
document.getElementById('myButton').onclick = function() {
  console.log('Button clicked:', this);  // Refers to the button element
};

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

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

10. What are template 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:

let name = "John";
let message = `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 String
let poem = "Roses are red,\nViolets are blue,\nSugar is sweet,\nAnd so are you.";

// Template Literal
let poemTemplate = `
  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 a core JavaScript feature. However, they may not work in older browsers such as Internet Explorer without transpilation or polyfilling.

11. What is a higher-order function in JavaScript?

A higher-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 as map, reduce, and filter. 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 like setTimeout and addEventListener 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: The map, filter, and reduce 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 function
function multiplier(factor) {
  return function(num) {
    return num * factor;
  };
}

// Invoke a higher-order function
const twice = multiplier(2);
console.log(twice(5));  // Output: 10

// Functional programming with higher-order functions
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(multiplier(2));  // [2, 4, 6, 8, 10]
const tripled = 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, meaning functions 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 function
function greet() {
  console.log('Hello!');
}

// Assign the function to a variable
let sayHello = greet;

// Call the function through the variable
sayHello();  // Output: "Hello!"

// Reassign the variable to a new function
sayHello = function() {
  console.log('Bonjour!');
};

// Call it again to see the new behavior
sayHello();  // 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 do functional 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, and declarative 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:

const sayHello = () => console.log('Hello!');
const runFunction = (func) => func();

runFunction(sayHello);  // Output: "Hello!"

14. What are IIFEs (Immediately Invoked Function Expressions)?

The Immediately 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(){
    var foo = '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 with closures, lay the groundwork for modular code organization by shielding private variables and functions.

Modern Alternatives

With the introduction of ES6 and its let and const 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.

    (function factorial(n){
        if (n <= 1) return 1;
        return n * 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 create private variables in JavaScript?

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

How Closures Work

A closure allows a function to retain access to the lexical 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 any inner function, defined inside another function, has access to the outer function's variables, and that access is maintained even after the outer function has finished executing.

For example:

function outerFunction() {
    let outerVar = 'I am outer';  // This variable is in the lexical environment of outerFunction

    function innerFunction() {
        console.log(outerVar);  // Accesses outerVar from the lexical environment of outerFunction
    }

    return innerFunction;
}

let myInnerFunction = outerFunction();
myInnerFunction();  // Logs: "I am outer"

Here, innerFunction retains access to outerVar.

Practical Implementation with Constructor Functions and Modules

Constructor Functions

When defining a JavaScript constructor function with function and new, closure can be used to associate private state with each instance:

function Gadget() {
    let secret = 'top secret';
    this.setSecret = function (value) {
        secret = value;
    };
    this.getSecret = function () {
        return secret;
    };
}

let phone = new Gadget();
phone.setSecret('new secret');
console.log(phone.getSecret());  // 'new secret'

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

Modules

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

  • The revealing module pattern enables selective exposure of private members.

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

Here is the code:

let myModule = (function () {
    let privateVariable = 'I am private';

    function privateMethod() {
        console.log('I am a private method');
    }

    return {
        publicMethod: function () {
            console.log('I am a public method');
        },
        getPrivateVariable: function () {
            return privateVariable;
        }
    };
})();

console.log(myModule.getPrivateVariable());  // 'I am private'
myModule.privateMethod();  // Throws an error because privateMethod is not exposed

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

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

Explore all 100 answers here ?? Devinterview.io - JavaScript


web-and-mobile-development

中耳炎什么症状 梦见狐狸是什么意思 鄂尔多斯为什么叫鬼城 女人得性瘾什么症状 两肺纹理增多模糊是什么意思
左旋肉碱是什么 嘴唇周围长痘痘是什么原因导致 肾综合征是什么病严重吗 6朵玫瑰代表什么意思 吃什么能让胸变大
什么牌子好 2001年是什么年 孩子拉肚子吃什么食物好 嘴唇干燥是什么原因 孕妇梦到老公出轨什么意思
牛油果是什么季节的水果 海底捞是什么 一什么玉米 6.8是什么星座 半枝莲有什么功效
吃什么对胰腺有好处hcv8jop0ns9r.cn 手抽筋吃什么药hcv8jop5ns9r.cn 吃黄体酮有什么副作用hcv9jop2ns2r.cn 干咳吃什么药效果好hcv9jop3ns1r.cn 支原体抗体阳性是什么意思chuanglingweilai.com
鱼条念什么hcv9jop6ns3r.cn 儿童鸡胸挂什么科hcv7jop5ns2r.cn 静脉曲张挂什么号0297y7.com 高位截瘫是什么意思hcv8jop6ns4r.cn 上海松江有什么好玩的地方hcv8jop7ns0r.cn
病毒疣是什么hcv8jop3ns1r.cn 晚上睡觉流口水是什么病xianpinbao.com 孕检唐筛是检查什么的hcv8jop0ns2r.cn 2型糖尿病是什么意思hcv7jop5ns1r.cn 鹅蛋炒香菜治什么病hcv9jop6ns8r.cn
专科学什么专业好就业hcv9jop3ns6r.cn 刚怀孕有什么特征hanqikai.com 滇红茶属于什么茶hcv8jop5ns6r.cn 元宵节吃什么hcv9jop4ns6r.cn 眼睛为什么会近视hcv9jop2ns9r.cn
百度