10 Common JavaScript Interview Questions for Beginners

Tomescu Dani
5 min readJul 27, 2023

--

JavaScript is a powerful and widely-used programming language that is mainly used for, but not limited to, web development.

As a beginner preparing for JavaScript interviews, it’s crucial to be familiar with fundamental concepts and be able to showcase your skills. In this article we’ll explore ten common interview questions for beginners with answers for each one of them

Let’s begin !

  1. What is JavaScript?

JavaScript is a lightweight, interpreted programming language that enables dynamic and interactive content on web pages. It runs directly in the browser and allows developers to manipulate HTML elements, handle events, and communicate with servers.

2. How do you declare variables in JavaScript?

In JavaScript, you can declare variables using var, let, or const. var has function-level scope, while let and const have block-level scope. Additionally, const variables cannot be reassigned after declaration.

// Using var (function-scoped)
function exampleVar() {
if (true) {
var x = 10;
}
console.log(x); // Output: 10
}

// Using let (block-scoped)
function exampleLet() {
if (true) {
let y = 5;
}
console.log(y); // Error: y is not defined
}

// Using const (block-scoped, cannot be reassigned)
const PI = 3.14;
PI = 3.14159; // Error: Assignment to constant variable

3. What are the data types in JavaScript?

JavaScript has six primitive data types: string, number, boolean, null, undefined, and symbol. Additionally, it has the complex data type object, which includes arrays, functions, and objects.

let name = "John"; // string
let age = 30; // number
let isStudent = true; // boolean

let person = null; // null
let city; // undefined

let hobbies = ["reading", "swimming", "coding"]; // array
let personObj = { name: "Alice", age: 25 }; // object

4. How do you check the type of a variable?

You can use the typeof operator to determine the type of a variable.

let x = 5;
console.log(typeof x); // Output: "number"

let y = "Hello";
console.log(typeof y); // Output: "string"

let z = true;
console.log(typeof z); // Output: "boolean"

let obj = { name: "John" };
console.log(typeof obj); // Output: "object"

let arr = [1, 2, 3];
console.log(typeof arr); // Output: "object"
  • Note that obj and arr return the same value ‘object’. If you want to specifically check whether arr is actually an array you must use “Array.isArray(arr)”

5. Explain reference types in JavaScript

In JavaScript, reference types are a category of data types that don’t store the actual data directly but instead hold a reference (memory address) to the location where the data is stored. When you assign a reference type to a variable, the variable contains a pointer to the memory location where the data is stored. This is different from primitive data types, which directly store the value itself.

The reference types in JavaScript are objects and arrays.

  • Object example:
// Creating an object
const person = {
name: "John",
age: 30,
city: "New York",
};

// Creating a new variable and assigning the object to it
const anotherPerson = person;

// Modifying the object through the new variable
anotherPerson.age = 35;

// The change is reflected in the original object
console.log(person.age); // Output: 35
  • Array example:
// Creating an array
const fruits = ["apple", "banana", "orange"];

// Creating a new variable and assigning the array to it
const otherFruits = fruits;

// Modifying the array through the new variable
otherFruits.push("grape");

// The change is reflected in the original array
console.log(fruits); // Output: ["apple", "banana", "orange", "grape"]

6. What is hoisting in JavaScript?

Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their scope during the compilation phase. However, only declarations are hoisted, not initializations.

console.log(x); // Output: undefined
var x = 5;

// The above code is equivalent to:
// var x;
// console.log(x);
// x = 5;
  • Note that only variables declared with ‘var’ keyword are hoisted. If you use let or const it will return an error

7. How do you handle errors in JavaScript?

The try-catch-finally statement allows you to handle errors gracefully. Code within the try block is executed, and if an error occurs, the catch block is triggered, allowing you to handle the error. The finally block executes regardless of whether an error occurred or not.

try {
// Code that may throw an error
let result = 10 / 0;
} catch (error) {
console.log("Error:", error); // Output: "Error: Infinity"
} finally {
console.log("Execution completed.");
}

// Output:
// "Error: Infinity"
// "Execution completed."

8. What are closures in JavaScript?

A closure is a function that retains access to variables from its lexical scope even after the outer function has finished executing. Closures are crucial for maintaining data privacy and creating “private” variables.

function outerFunction() {
let counter = 0
return function innerFunction() {
counter++;
console.log(counter)
};
}

const innerFunc = outerFunction();
innerFunc(); // Output: 1
innerFunc(); // Output: 2

9. How can you iterate over objects and arrays in JavaScript?

There are several ways to iterate through object and arrays, such as ‘for…in’, ‘for…of’ loop, and array methods like ‘forEach’, ‘map’, ‘filter’, ‘reduce’

  • Using the classic ‘for’ loop:
const colors = ["red", "green", "blue"];
for (let i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
  • Using ‘for…of’ loop:
const colors = ["red", "green", "blue"];
for (const color of colors) {
console.log(color);
}
  • Using ‘for…in’ loop for objects:
const person = {
name: "John",
age: 30,
city: "New York",
};

for (const key in person) {
console.log(`key: ${key}, value: ${person[key]}`); // key: name, value: John
}
  • Using forEach (note that forEach does not return a new array):
const colors = ["red", "green", "blue"];
colors.forEach((color) => {
console.log(color);
});
  • Using map (returns a new array):
const numbers = [1, 2, 3];
const squares = numbers.map((num) => num * num);
console.log(squares); // Output: [1, 4, 9]
  • Using ‘filter’
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter((num) => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]
  • Using ‘reduce’:
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum); // Output: 15

10. What is the difference between ‘==’ and ‘===’ in JavaScript?

In JavaScript, == (loose equality) and === (strict equality) are comparison operators used to compare values. The double equal (==) only compares the value and the triple equal (===) compares the type AND the value

console.log(5 == "5"); // Output: true
console.log(5 === "5"); // Output: false
console.log(null == undefined); // Output: true
console.log(0 == false); // Output: true
console.log(0 === false); // Output: false

Follow for more

☕ Buy me a coffee

--

--

Tomescu Dani

Full Stack Engineer with a deep passion for Web and Mobile Development. I mainly use JavaScript-based technologies (React.js, Node.js, React Native)