JavaScript Interview Questions

23 Questions
JavaScript

JavaScript

Web DevelopmentFrontendBackend

Question 13

What are the primitive data types in JavaScript?

Answer:

The primitive data types in JavaScript include string, number, boolean, null, undefined, and symbol. These are the most basic types of data that are immutable, meaning their value cannot be changed. Primitive values are compared by value, while objects (which are not primitive) are compared by reference.

Here is an example demonstrating each primitive data type:

// String
let name = 'John';
console.log(typeof name); // 'string'

// Number
let age = 30;
console.log(typeof age); // 'number'

// Boolean
let isMarried = true;
console.log(typeof isMarried); // 'boolean'

// Null
let car = null;
console.log(typeof car); // 'object' (this is a known quirk)

// Undefined
let address;
console.log(typeof address); // 'undefined'

// Symbol
let sym = Symbol('symbol');
console.log(typeof sym); // 'symbol'

In this example, we declare and log the type of each primitive data type, showing the different ways data can be represented and manipulated in JavaScript.

Recent job openings