JavaScript is one of the most popular programming languages for web development. If you're preparing for a JavaScript interview as a fresher, it's essential to know the basics as well as some common coding concepts. Here’s a list of the top 20 JavaScript interview questions along with their answers.
JavaScript is a high-level, interpreted programming language used to make web pages interactive. It is an essential part of web development alongside HTML and CSS.
JavaScript has the following primitive data types:
String
Number
Boolean
Undefined
Null
Symbol
BigInt
var
, let
, and const
?var
has function scope and can be redeclared.
let
has block scope and cannot be redeclared within the same scope.
const
is also block-scoped but cannot be reassigned after initialization.
==
and ===
.==
checks for equality but allows type conversion.
===
checks for both value and type equality (strict equality).
A function is a reusable block of code that performs a specific task.
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("John"));
Arrow functions provide a shorter syntax for writing functions.
const add = (a, b) => a + b;
console.log(add(2, 3));
A callback function is a function passed as an argument to another function and executed later.
function fetchData(callback) {
setTimeout(() => {
console.log("Data fetched");
callback();
}, 1000);
}
fetchData(() => console.log("Callback executed"));
An IIFE is a function that executes immediately after its definition.
(function() {
console.log("IIFE executed");
})();
null
and undefined
?null
is an assigned value representing no value.
undefined
means a variable has been declared but not assigned a value.
this
keyword in JavaScript?this
refers to the object that is executing the function. Its value depends on how the function is called.
Template literals allow embedding expressions inside strings using backticks:
const name = "John";
console.log(`Hello, ${name}!`);
Promises are used to handle asynchronous operations.
let myPromise = new Promise((resolve, reject) => {
setTimeout(() => resolve("Promise resolved!"), 2000);
});
myPromise.then(value => console.log(value));
Synchronous code executes line by line.
Asynchronous code allows tasks to run in the background without blocking execution.
map()
function in JavaScript?The map()
function creates a new array by applying a function to each element of an existing array.
const numbers = [1, 2, 3];
const squared = numbers.map(num => num * num);
console.log(squared);
Event delegation allows handling events at a parent level instead of attaching listeners to individual elements.
typeof
operator?The typeof
operator returns the data type of a variable.
console.log(typeof 10); // "number"
console.log(typeof "hello"); // "string"
A higher-order function takes another function as an argument or returns a function.
function operate(operation, a, b) {
return operation(a, b);
}
const multiply = (x, y) => x * y;
console.log(operate(multiply, 2, 3));
Destructuring allows extracting values from arrays or objects easily.
const person = { name: "John", age: 30 };
const { name, age } = person;
console.log(name, age);
A closure is a function that remembers variables from its outer scope even after the outer function has executed.
function outerFunction(x) {
return function innerFunction(y) {
return x + y;
};
}
const addFive = outerFunction(5);
console.log(addFive(10));
Modules allow breaking JavaScript code into reusable files using export
and import
.
// module.js
export function greet() {
return "Hello!";
}
// main.js
import { greet } from "./module.js";
console.log(greet());
Preparing for JavaScript interviews as a fresher can be overwhelming, but understanding these fundamental questions will help you gain confidence. Make sure to practice coding and build small projects to solidify your knowledge. Good luck with your JavaScript interview!