Essential JavaScript Interview Questions: Part 1

Madushan Ranasinghe
4 min readJun 15, 2024

--

So you wat to learn JavaScript and Become a Software Developer right. But can you answer these questions. In this article series, I will be going through from beginning to most advance JavaScript interview questions along with detailed answers and code examples. This will be the first part of the series.

1. What are the data types in JavaScript?

JavaScript has six primitive data types: string, number, Boolean, null, undefined, and symbol, along with a complex data type called object.

2. What are features of JavaScript?

  • Input validation : JavaScript allows you to validate user input before sending it to server for backend operations.
  • Control over browser : JavaScript gives more control over browser due to which you can change the background colour of this page as well as the text on the browser’s status bar.
  • Detect browser and OS : JavaScript enables you to detect user’s browser and OS due to which you can perform platform dependent operations.
  • Handling date and time : JavaScript enables you to write code based users date and time, it also enables you to capture users date and time as user and server may be in different time zone.
  • Generating HTML on fly : JavaScript enables you to dynamically generate HTML.

2. What is the difference between null and undefined?

null represents the intentional absence of any object value, while undefined indicates the absence of a value or an uninitialized variable.

let a; // a is declared but not initialized
console.log(a); // undefined

function example() {
let b = 10;
}
let result = example(); // result is not explicitly returned
console.log(result); // undefined

let c = null; // c is explicitly set to null
console.log(c); // null

function findUser(id) {
if (id !== 1) {
return null; // Explicitly returning null to indicate no user found
}
return { id: 1, name: 'John Doe' };
}
let user = findUser(2);
console.log(user); // null

3. What is the DOM in JavaScript?

The Document Object Model (DOM) is a programming interface that represents the structure of HTML and XML documents. It allows JavaScript to access and manipulate the content and structure of a webpage.

4. What is an event in JavaScript?

An event is an action or occurrence that happens in the browser, such as a button click or page load. JavaScript can respond to these events by executing code in response.

5. What is JavaScript engine?

JavaScript engine is a computer program used to execute JavaScript code. JS engines were developed by web browser vendors and every major browser has one. Chrome V8 from google is most used engine, Google chrome use it.

6. What is var?

The var statement declares a variable and can also optionally initialize its value. Variables are declared using var as below

var z = 1;
console.log(z); // 1

7. What is let?

The ‘let’ allows you to declare variables that are limited in scope to the particular block, expression on which it is used or statement. Variables are declared using let as below

let b = 3;
console.log(b); // 3

8. What is const?

Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through reassignment, and it can’t be re-declared.

const pi = 3.14159;
console.log(pi); // 3.14159

// Attempting to re-assign a const variable will result in an error
// pi = 3; // TypeError: Assignment to constant variable.

// However, the value of a const object can still be changed
const person = { name: 'Alice' };
person.name = 'Bob'; // This is allowed
console.log(person); // { name: 'Bob' }

9. What is difference between let and var?

The variable defined with var is available anywhere within the function hence ’var’ keyword has function scope. The let has a Block Scope. A variable declared with ‘let’ keyword has a scope only with in that block.

function example() {
if (true) {
var x = 10;
}
console.log(x); // 10
}
example();

function example() {
if (true) {
let y = 20;
console.log(y); // 20
}
console.log(y); // ReferenceError: y is not defined
}
example();

10. What is difference between let and const?

‘let’ allows you change the value of variable any number of times. Using ‘const’, after the first assignment of the value we cannot redefine the value again.

let x = 10;
x = 20; // No error
console.log(x); // 20

const y = 30;
// y = 40; // TypeError: Assignment to constant variable.
console.log(y); // 30

--

--

Madushan Ranasinghe

Engineering Undergraduate who loves to explore new technologies.