Lädt...


🔧 JavaScript Interview Questions


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Here are some commonly asked JavaScript interview questions along with their answers:

1. What is the difference between var, let, and const?

  • var: Function-scoped, can be redeclared, hoisted but not block-scoped.
  • let: Block-scoped, cannot be redeclared within the same block, hoisted but not initialized.
  • const: Block-scoped, must be initialized at the time of declaration, value cannot be reassigned.

Example:

function example() {
  if (true) {
    var x = 10; // accessible outside this block
    let y = 20; // block-scoped
    const z = 30; // block-scoped and immutable
  }
  console.log(x); // 10
  // console.log(y); // Error
  // console.log(z); // Error
}

2. What is a closure, and how is it used?

  • A closure is when a function "remembers" the variables from its lexical scope even when the function is executed outside that scope.

Example:

function outer() {
  let count = 0;
  return function inner() {
    count++;
    return count;
  };
}

const counter = outer();
console.log(counter()); // 1
console.log(counter()); // 2
console.log(counter()); // 3

3. Explain the difference between == and ===.

  • ==: Compares values after type coercion.
  • ===: Compares values and types strictly without type coercion.

Example:

console.log(5 == '5');  // true (type coercion)
console.log(5 === '5'); // false (strict comparison)

4. How does the JavaScript Event Loop work?

  • The event loop allows JavaScript to perform non-blocking operations by using a single-threaded model.
  • Call Stack: Executes code.
  • Task Queue: Stores asynchronous callbacks like setTimeout.
  • Microtask Queue: Stores promises (.then, catch).
  • Event Loop: Monitors the stack and pushes tasks from the queue when the stack is empty.

Example:

console.log('Start');

setTimeout(() => console.log('Timeout'), 0);

Promise.resolve().then(() => console.log('Promise'));

console.log('End');

// Output: Start, End, Promise, Timeout

5. What is the purpose of bind(), call(), and apply()?

These methods are used to change the this context of a function:

  • bind(): Returns a new function with the specified this.
  • call(): Invokes the function immediately with a specified this and arguments.
  • apply(): Similar to call() but takes arguments as an array.

Example:

const obj = { name: 'Alice' };

function greet(greeting) {
  return `${greeting}, ${this.name}`;
}

console.log(greet.call(obj, 'Hello')); // Hello, Alice
console.log(greet.apply(obj, ['Hi'])); // Hi, Alice
const boundGreet = greet.bind(obj);
console.log(boundGreet('Hey')); // Hey, Alice

6. Write a function to check if a string is a palindrome.

A palindrome reads the same backward as forward.

function isPalindrome(str) {
  const reversed = str.split('').reverse().join('');
  return str === reversed;
}

console.log(isPalindrome('madam')); // true
console.log(isPalindrome('hello')); // false

7. What is the difference between Object.create() and using the new keyword?

  • Object.create(): Creates a new object with a specified prototype.
  • new keyword: Creates an instance of a constructor function and sets the prototype.

Example:

const parent = { greet: () => 'Hello' };
const child = Object.create(parent);
console.log(child.greet()); // Hello

function Person(name) {
  this.name = name;
}
const person = new Person('John');
console.log(person.name); // John

8. Implement a custom Array.prototype.map function.

Array.prototype.myMap = function(callback) {
  const result = [];
  for (let i = 0; i < this.length; i++) {
    result.push(callback(this[i], i, this));
  }
  return result;
};

const arr = [1, 2, 3];
const doubled = arr.myMap(num => num * 2);
console.log(doubled); // [2, 4, 6]

9. How do debouncing and throttling work?

  • Debouncing: Ensures a function is executed only after a specified delay after the last call.
  • Throttling: Ensures a function is executed at most once in a specified interval.

Debouncing Example:

function debounce(fn, delay) {
  let timer;
  return function(...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), delay);
  };
}

const debouncedFn = debounce(() => console.log('Debounced!'), 300);
debouncedFn();
debouncedFn(); // Will log "Debounced!" only once after 300ms.

Throttling Example:

function throttle(fn, interval) {
  let lastCall = 0;
  return function(...args) {
    const now = Date.now();
    if (now - lastCall >= interval) {
      lastCall = now;
      fn.apply(this, args);
    }
  };
}

const throttledFn = throttle(() => console.log('Throttled!'), 1000);
throttledFn();
throttledFn(); // Will log "Throttled!" at most once per 1000ms.
...

🔧 Commonly asked ReactJS interview questions. Here are ReactJS interview questions and answers


📈 32.15 Punkte
🔧 Programmierung

🔧 【Interview Essentials】Common JavaScript Interview Questions


📈 28.93 Punkte
🔧 Programmierung

🔧 Technical Interview Questions - Behavioral Questions


📈 24.65 Punkte
🔧 Programmierung

🔧 Top 20 ReactJS Questions interview questions to get a job


📈 24.65 Punkte
🔧 Programmierung

🔧 【Interview Essentials】8 Common TypeScript Interview Questions


📈 23.58 Punkte
🔧 Programmierung

🔧 Prepare for Interview Like a Pro with Interview Questions CLI


📈 23.58 Punkte
🔧 Programmierung

🔧 Ruby on Rails Interview Prep: Key Questions to Expect in Your Interview


📈 23.58 Punkte
🔧 Programmierung

🔧 Top Node.js Interview Questions to Ace Your Next Tech Interview


📈 23.58 Punkte
🔧 Programmierung

🔧 JavaScript Interview Questions


📈 21.43 Punkte
🔧 Programmierung

🔧 JavaScript Interview Questions


📈 21.43 Punkte
🔧 Programmierung

🔧 50 Essential JavaScript Interview Questions You Need to Know


📈 21.43 Punkte
🔧 Programmierung

🔧 Top 10 Interview Questions And Answers In JavaScript For 2025


📈 21.43 Punkte
🔧 Programmierung

🔧 Top 20 Advanced JavaScript Interview Questions and Answers for Seasoned Engineers


📈 21.43 Punkte
🔧 Programmierung

🔧 50 Essential JavaScript Interview Questions You Need to Know


📈 21.43 Punkte
🔧 Programmierung

🔧 Top 20 JavaScript Interview Questions for 2025


📈 21.43 Punkte
🔧 Programmierung

🔧 Top JavaScript Interview Questions for 5+ Years of Experience


📈 21.43 Punkte
🔧 Programmierung

🔧 javascript string manipulation Interview Questions


📈 21.43 Punkte
🔧 Programmierung

🔧 5 Javascript coding interview questions - Part 6


📈 21.43 Punkte
🔧 Programmierung

🔧 JavaScript Memory Management Interview Questions


📈 21.43 Punkte
🔧 Programmierung

🔧 Get Ready for Your JavaScript Interview: Top 100 Questions to Practice


📈 21.43 Punkte
🔧 Programmierung

🔧 Some commonly asked JavaScript array-related interview questions along with their answers


📈 21.43 Punkte
🔧 Programmierung

🔧 Top JavaScript Interview Questions for 2 Years of Experience


📈 21.43 Punkte
🔧 Programmierung

🔧 JavaScript Coding Interview Questions


📈 21.43 Punkte
🔧 Programmierung

🔧 JavaScript Closures: Top Interview Questions and Answers


📈 21.43 Punkte
🔧 Programmierung

🔧 Top 10 Expert-Crafted JavaScript Coding Interview Questions


📈 21.43 Punkte
🔧 Programmierung

🔧 JavaScript Interview Questions and Answers


📈 21.43 Punkte
🔧 Programmierung

🔧 25 JavaScript Concept Interview Questions with Examples


📈 21.43 Punkte
🔧 Programmierung

🔧 Top 10 Expert-Crafted JavaScript Coding Interview Questions


📈 21.43 Punkte
🔧 Programmierung

🔧 5 JavaScript Interview Questions


📈 21.43 Punkte
🔧 Programmierung

🔧 Top Challenging and hardest javascript technical interview questions with solutions.


📈 21.43 Punkte
🔧 Programmierung

🔧 Must-know Basic JavaScript Interview Questions for Freshers


📈 21.43 Punkte
🔧 Programmierung

🔧 JavaScript Interview Questions


📈 21.43 Punkte
🔧 Programmierung

matomo