JavaScript Flashcards

Category sponsor

JavaScript is a versatile, dynamic programming language that is a fundamental part of modern web development. Initially created to add interactivity to static HTML pages, JavaScript has evolved into a powerful tool that can be used both on the client and server sides.

Our flashcard app includes 170 carefully selected JavaScript interview questions with comprehensive answers that will effectively prepare you for any interview requiring JS knowledge. IT Flashcards is not just a tool for job seekers - it's a great way to reinforce and test your knowledge, regardless of your current career plans. Regular use of the app will help you stay up-to-date with the latest JavaScript trends and keep your skills at a high level.

Sample JavaScript flashcards from our app

Download our app from the App Store or Google Play to get more free flashcards or subscribe for access to all flashcards.

JavaScript

What is lexical scope in JavaScript?

Lexical scope in JavaScript is a principle where the visibility range of a variable is determined by its location in the code. This means that variables are accessible inside the block in which they were defined, as well as in any nested blocks. This enables the creation of closures and control over variable access. Example of using lexical scope in JavaScript code:
function outerFunction() {
  let outerVariable = `I'm outside!`;

  function innerFunction() {
    console.log(outerVariable); // Has access to the 'outerVariable'
  }

  innerFunction();
}
outerFunction(); // Displays `I'm outside!`

Lexical scope allows an inner function to access variables defined in an outer function, even after the outer function has finished. This is a key element in creating closures in JavaScript, allowing for more flexible state management in applications.

JavaScript

What is hoisting?

Hoisting is a mechanism in JavaScript languages where variables and functions are moved to the top of their scope before the code is executed. In practice, this means that we can use functions or variables before they are actually declared.

However, it should be noted that hoisting works slightly differently for variables and functions.

For variables declared with var keywords, only the declaration is hoisted, not the initialization. Variables initialized before declaration will be returned as undefined.

An example of hoisting code for variables:
console.log(myVar); // undefined
var myVar = 5;
console.log(myVar); // 5

For functions, hoisting moves both the declaration and definition of the function to the top, which allows the use of the function before it's declared.

An example of hoisting code for functions:
console.log(myFunction()); // "Hello World"

function myFunction() {
  return "Hello World";
}

Hoisting does not occur for variables declared with let and const.

JavaScript

What is an arrow function and what are its advantages?

An arrow function, also known as arrow function, is a type of function introduced in ECMAScript 6 (ES6). They are called arrow functions because they use a special syntax with an arrow ( => ) to define the function.

For comparison, a traditional function might look like this:
function sum(a, b) {
  return a + b;
}

Its equivalent as an arrow function is:
const sum = (a, b) => a + b;

The main benefit of an arrow function is that it doesn't create its own execution context (binding to this), which is often a source of errors in JavaScript. In arrow functions, this is inherited from the surrounding context. Another advantage is the brevity of syntax, especially useful when functions are used as arguments to other functions, e.g. in higher-order functions.

On the other hand, due to the lack of its own this, arrow functions are not suitable for defining constructor (creative) objects or for creating methods in prototype objects.

JavaScript

What is a Promise object and how can it be used?

The Promise object in JavaScript is used to handle asynchronous operations. A Promise represents a value that may not be available at the time the Promise is created, but may be available in the future, or never at all.

A Promise object can be in one of three states:
1. Pending - operation is still ongoing, not finished either successfully or with errors.
2. Fulfilled - operation completed successfully, Promise returned a value.
3. Rejected - operation completed with an error, Promise returned the reason for the error.

A Promise that has been fulfilled or rejected is considered "settled" and its state never changes.

Creating a Promise object:
const promise = new Promise((resolve, reject) => {
  const success = true;
  if (success) {
    resolve('Operation successful.');
  } else {
    reject('Operation failed.');
  }
});

Using a Promise object:
promise
  .then(result => {
    console.log(result); // Will print: 'Operation successful.'
  })
  .catch(error => {
    console.log(error);
  });

The .then() method is executed when the Promise is fulfilled, and .catch() when it is rejected. In both cases, the result of the operation or the reason for the rejection of the Promise is passed as an argument.

Download IT Flashcards App Now

Empower your IT learning journey with the ultimate flashcard system. From basic programming principles to mastering advanced technologies, IT Flashcards is your passport to IT excellence. Download now and unlock your potential in today's competitive tech landscape.

Home Blog Sponsors Contact Privacy Policy Terms of Service

Copyright © 2025 IT Flashcards