Top 30 Javascript |Basic questions | For Interview

Top 30 Javascript |Basic questions | For Interview

Interview Question

Table of contents

  1. What is JavaScript?

JavaScript is a high-level programming language used for creating dynamic and interactive web pages.

  1. What is the difference between JavaScript and Java?

JavaScript and Java are completely different languages. Java is an object-oriented programming language, while JavaScript is a scripting language primarily used for web development.

  1. What are the data types in JavaScript?

There are seven data types in JavaScript: string, number, boolean, null, undefined, symbol, and object.

Example:

javascriptCopy codelet str = "hello";
let num = 10;
let bool = true;
let n = null;
let u = undefined;
let sym = Symbol();
let obj = {name: "John"};
  1. What is the difference between let, var, and const in JavaScript?

The main difference between let, var, and const is that let and const have block scope, while var has function scope.

Example:

javascriptCopy code{
   let x = 10;
   var y = 20;
   const z = 30;
}
console.log(x); // ReferenceError: x is not defined
console.log(y); // 20
console.log(z); // ReferenceError: z is not defined
  1. What is the difference between == and === in JavaScript?

The == operator checks for value equality, while the === operator checks for both value and type equality.

Example:

javascriptCopy codeconsole.log(1 == '1'); // true
console.log(1 === '1'); // false
  1. What is the difference between null and undefined in JavaScript?

Null is an assigned value that represents no value or empty value, while undefined means a variable has been declared but has not yet been assigned a value.

Example:

javascriptCopy codelet x = null;
let y;
console.log(x); // null
console.log(y); // undefined
  1. What is hoisting in JavaScript?

Hoisting is a mechanism in JavaScript where variables and function declarations are moved to the top of their respective scopes before code execution.

Example:

javascriptCopy codeconsole.log(x); // undefined
var x = 10;
  1. What is closure in JavaScript?

A closure is a function that has access to its outer function's variables, even after the outer function has returned.

Example:

scssCopy codefunction outer() {
   let x = 10;
   function inner() {
      console.log(x);
   }
   return inner;
}
let closure = outer();
closure(); // 10
  1. What are higher-order functions in JavaScript?

Higher-order functions are functions that take one or more functions as arguments or return a function as a result.

Example:

javascriptCopy codefunction add(x, y) {
   return x + y;
}
function higherOrder(func) {
   let a = 10;
   let b = 20;
   return func(a, b);
}
console.log(higherOrder(add)); // 30
  1. What is the prototype in JavaScript?

The prototype is an object that is associated with every function and object by default in JavaScript, which allows inheritance and sharing of properties and methods between objects.

Example:

javascriptCopy codefunction Person(name, age) {
   this.name = name;
   this.age = age;
}
Person.prototype.sayHello = function() {
   console.log(`Hello, my name is ${this.name}`);
};
let person1 = new Person('John', 25);
person1.sayHello(); // Hello, my name is John
  1. What is the difference between call and apply in JavaScript?

The call() method takes arguments separately, while the apply() method takes arguments as an array.

Example:

javascriptCopy codefunction greet(message) {
   console.log(`${message}, ${this.name}`);
}
let person = {name:
  1. What is an event in JavaScript?

An event is an action or occurrence that happens in the browser, such as a button click, a page load, or a mouse movement.

Example:

javascriptCopy codelet button = document.querySelector('button');
button.addEventListener('click', function() {
   console.log('Button clicked');
});
  1. What is the difference between synchronous and asynchronous code in JavaScript?

Synchronous code is executed in a single thread and blocks the execution until the code is completed, while asynchronous code allows the execution of multiple threads and does not block the execution.

Example:

javascriptCopy code// Synchronous code
function add(x, y) {
   return x + y;
}
let result = add(2, 3);
console.log(result);

// Asynchronous code
setTimeout(function() {
   console.log('Hello');
}, 1000);
  1. What is a callback function in JavaScript?

A callback function is a function that is passed as an argument to another function and is executed when the first function is completed.

Example:

scssCopy codefunction add(x, y, callback) {
   let result = x + y;
   callback(result);
}
function display(result) {
   console.log(result);
}
add(2, 3, display); // 5
  1. What is a Promise in JavaScript?

A Promise is an object that represents the eventual completion or failure of an asynchronous operation and allows chaining of multiple asynchronous operations.

Example:

javascriptCopy codelet promise = new Promise(function(resolve, reject) {
   setTimeout(function() {
      resolve('Success');
   }, 2000);
});
promise.then(function(result) {
   console.log(result);
}).catch(function(error) {
   console.log(error);
});
  1. What is async/await in JavaScript?

Async/await is a new feature in JavaScript that allows writing asynchronous code in a synchronous style using the async and await keywords.

Example:

javascriptCopy codeasync function getData() {
   let response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
   let data = await response.json();
   console.log(data);
}
getData();
  1. What is the spread operator in JavaScript?

The spread operator is an ES6 feature that allows an iterable, such as an array or string, to be expanded into individual elements.

Example:

javascriptCopy codelet arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let arr3 = [...arr1, ...arr2];
console.log(arr3); // [1, 2, 3, 4, 5, 6]
  1. What is destructuring in JavaScript?

Destructuring is a way of extracting values from objects or arrays and assigning them to variables.

Example:

javascriptCopy codelet person = {name: 'John', age: 25};
let {name, age} = person;
console.log(name); // John
console.log(age); // 25
  1. What are arrow functions in JavaScript?

Arrow functions are a shorthand syntax for creating functions in JavaScript, which have a more concise syntax and automatically bind to the surrounding context.

Example:

javascriptCopy codelet add = (x, y) => x + y;
console.log(add(2, 3)); // 5
  1. What is the difference between map and forEach in JavaScript?

The map() method returns a new array after applying a function to each element, while the forEach() method simply loops through the array and applies a function to each element.

Example:

javascriptCopy codelet arr = [1, 2, 3];
let newArr1 = arr.map(x => x * 2);
let newArr2 = [];
  1. What is the difference between == and === in JavaScript?

The == operator compares the value of two variables, while the === operator compares both the value and type of the variables.

Example:

javascriptCopy codeconsole.log(1 == '1'); // true
console.log(1 === '1'); // false
  1. What is a closure in JavaScript?

A closure is a function that has access to variables in its outer function, even after the outer function has returned.

Example:

javascriptCopy codefunction outer() {
   let name = 'John';
   function inner() {
      console.log(name);
   }
   return inner;
}
let func = outer();
func(); // John
  1. What is the difference between let and var in JavaScript?

The let keyword creates a block-scoped variable, while the var keyword creates a function-scoped variable.

Example:

javascriptCopy codefunction test() {
   var x = 1;
   if (true) {
      let y = 2;
      console.log(y); // 2
   }
   console.log(x); // 1
   console.log(y); // ReferenceError: y is not defined
}
  1. What is the difference between null and undefined in JavaScript?

Undefined is a variable that has been declared but has not been assigned a value, while null is an assignment value representing no value or no object.

Example:

javascriptCopy codelet x;
console.log(x); // undefined

let y = null;
console.log(y); // null
  1. What is hoisting in JavaScript?

Hoisting is a mechanism in JavaScript where variables and function declarations are moved to the top of their respective scopes.

Example:

javascriptCopy codeconsole.log(x); // undefined
var x = 1;

test(); // Hello
function test() {
   console.log('Hello');
}
  1. What is the difference between an object and an array in JavaScript?

An object is a collection of properties, while an array is a collection of elements.

Example:

javascriptCopy codelet person = {name: 'John', age: 25};
let arr = [1, 2, 3];
console.log(person.name); // John
console.log(arr[1]); // 2
  1. What is a constructor function in JavaScript?

A constructor function is a special function that is used to create objects with the same properties and methods.

Example:

javascriptCopy codefunction Person(name, age) {
   this.name = name;
   this.age = age;
   this.display = function() {
      console.log(this.name, this.age);
   }
}
let person1 = new Person('John', 25);
person1.display(); // John 25
  1. What is the difference between slice and splice in JavaScript?

The slice() method returns a new array by copying a portion of an existing array, while the splice() method adds or removes elements from an array.

Example:

javascriptCopy codelet arr = [1, 2, 3, 4, 5];
let newArr1 = arr.slice(1, 3);
console.log(newArr1); // [2, 3]

let newArr2 = arr.splice(1, 2);
console.log(newArr2); // [2, 3]
console.log(arr); // [1, 4, 5]
  1. What is the difference between reduce and filter in JavaScript?

The reduce() method applies a function to each element of an array and returns a single value, while the filter() method creates a new array with all elements that pass a test function.

Example:

// using filter

let arr = [1, 2, 3, 4, 5];
let newArr = arr.filter(function(num) {
   return num > 2;
});
console.log(newArr); // [3, 4, 5]

// using reduce

let arr = [1, 2, 3, 4, 5];
let sum = arr.reduce(function(accumulator, currentValue) {
   return accumulator + currentValue;
});
console.log(sum); // 15
  1. What is the difference between synchronous and asynchronous programming in JavaScript?

Synchronous programming is a blocking programming model where the program waits for a task to complete before moving on to the next task, while asynchronous programming is a non-blocking programming model where the program moves on to the next task while waiting for a previous task to complete.

Example:

javascriptCopy code// Synchronous example
function synchronous() {
   console.log('Task 1');
   console.log('Task 2');
   console.log('Task 3');
}
synchronous();

// Asynchronous example
function asynchronous() {
   console.log('Task 1');
   setTimeout(function() {
      console.log('Task 2');
   }, 2000);
   console.log('Task 3');
}
asynchronous();

Bonus question.

What is a promise in JavaScript?

A promise is an object that represents the eventual completion or failure of an asynchronous operation and its resulting value.

Example:

javascriptCopy codelet promise = new Promise(function(resolve, reject) {
   setTimeout(function() {
      let data = {name: 'John', age: 25};
      resolve(data);
   }, 2000);
});

promise.then(function(data) {
   console.log(data); // {name: 'John', age: 25}
}).catch(function(error) {
   console.log(error);
});

Did you find this article valuable?

Support shiv singh baghel by becoming a sponsor. Any amount is appreciated!