Skip to main content

Data Types - JavaScript Interview Questions

JavaScript Fundamentals: JavaScript Data Types




In JavaScript, how many data types are there?

View Answer:
Interview Response: JavaScript has eight fundamental data types: Number, String, Boolean, BigInt, Null, Undefined, Symbol, and Object. The first seven are primitive data types, meaning their values can only contain one item (a string or a number), while objects can store data sets and more complex things.

Code Example:

let myString = "Hello, world!"; // string
let myNumber = 42; // number
let myBoolean = true; // boolean
let myNull = null; // null
let myUndefined = undefined; // undefined
let mySymbol = Symbol("mySymbol"); // symbol
let myBigInt = 123456789n; // bigint

Which operator is used to determine the type of a particular value or argument?

View Answer:
Interview Response: The typeof operator.

Technical Response: In JavaScript, the typeof operator is used to determine the data type of a specific value or argument. It returns a string indicating the data type of the operand.

JavaScript supports two forms of syntax:

  1. As an operator: typeof x.
  2. As a function: typeof(x).
Code Example:

let myString = "Hello, world!";
let myNumber = 42;
let myBoolean = true;
let myNull = null;
let myUndefined = undefined;
let mySymbol = Symbol("mySymbol");
let myBigInt = 123456789n;

console.log(typeof myString); // Output: string
console.log(typeof myNumber); // Output: number
console.log(typeof myBoolean); // Output: boolean
console.log(typeof myNull); // Output: object
console.log(typeof myUndefined); // Output: undefined
console.log(typeof mySymbol); // Output: symbol

What kind of numbers are represented by the number type in JavaScript?

View Answer:
Interview Response: The number type represents numeric data, including integers and floating-point numbers. It also supports special numeric values such as Infinity and NaN.

Code Example:

let myInteger = 42; // integer
let myFloat = 3.14159; // floating-point

console.log(typeof myInteger); // Output: number
console.log(typeof myFloat); // Output: number

What is the limitation of the number data type?

View Answer:
Interview Response: In JavaScript, the limitation of the number type is that it can only represent numbers within a certain range, and it has limited precision when dealing with decimal fractions.

Technical Response: The “number” type cannot represent integer values greater than (253-1) (that’s 9007199254740991) or less than -(253-1) for negatives. It is a technical limitation caused by their internal representation, and anything beyond these values is considered a BigInt.

Code Example:

let myNumber = 0.1 + 0.2; // 0.30000000000000004

console.log(myNumber); // Output: 0.30000000000000004

To mitigate this issue, JavaScript provides a method called toFixed(), which allows you to specify the number of decimal places to include in a number:

let myNumber = 0.1 + 0.2; // 0.30000000000000004

console.log(myNumber.toFixed(2)); // Output: 0.30

How is a BigInt instantiated (created) in JavaScript?

View Answer:
Interview Response: In JavaScript, BigInts can be instantiated by appending "n" to the end of an integer literal or by calling the BigInt() constructor with an integer or a string representing an integer as its argument.

Which are the three types of quotes that can be used to create a string representation of a value in JavaScript?

View Answer:
Interview Response: Double, single, and backticks.

Technical Response: In JavaScript, there are three types of quotes that can be used to create a string representation of a value: single quotes, double quotes, and backticks (template literals).

Here's an example of how to use each type of quote:

let mySingleQuoteString = 'Hello, world!'; // using single quotes
let myDoubleQuoteString = "Hello, world!"; // using double quotes
let myBacktickString = `Hello, world!`; // using backticks

console.log(mySingleQuoteString); // Output: Hello, world!
console.log(myDoubleQuoteString); // Output: Hello, world!
console.log(myBacktickString); // Output: Hello, world!

All three types of quotes can be used interchangeably to create string literals. However, backticks have additional functionality that allows you to embed expressions directly in a string using template literals:

let myName = "Alice";
let myTemplateLiteralString = `Hello, ${myName}!`; // using template literals

console.log(myTemplateLiteralString); // Output: Hello, Alice!

In this example, we've used backticks to create a template literal string that includes an expression (${myName}) which is evaluated and embedded directly in the string.


What is the difference between double, single, and backtick quotes?

View Answer:
Interview Response: In JavaScript, double and single quotes are interchangeable and can be used to create string literals. Backtick quotes (template literals) allow embedding expressions and special characters. They allow us to embed variables and expressions inside a string, for example, by surrounding them with ${…}.


Code Example:

  1. Single quotes ('') and double quotes ("") work virtually identically. They are used to enclose and define string data.
let singleQuote = 'Hello';
let doubleQuote = "JavaScript!";

console.log(singleQuote + " " + doubleQuote); // Hello, JavaScript!
  1. Backticks (` `) were introduced in ES6. They allow for string interpolation (template literals) and multi-line strings.
let name = 'JavaScript';
let greeting = `Hello, ${name}!`; // Hello, JavaScript!
let multiLine = `Hello,
World!`;

Remember, all three can be used to define strings, but only backticks offer string interpolation and multi-line features.


note

Please note that we can only implement this with backticks in JavaScript. Other quotes (single and double) do not have this embedding functionality! We should make every effort to understand backticks and template literals.


What are the two possible values of the Boolean data type?

View Answer:
Interview Response: The Boolean logical type in JavaScript has two values: true and false. It is commonly used for conditional statements, loops, and comparisons in programming.

Code Example:Here's an example of how to use the Boolean data type...

let myBooleanTrue = true; // true
let myBooleanFalse = false; // false

console.log(typeof myBooleanTrue); // Output: boolean
console.log(typeof myBooleanFalse); // Output: boolean

Boolean values are commonly used in conditional statements to control the flow of a program. For example:

let myNumber = 42;

if (myNumber > 10) {
console.log("The number is greater than 10.");
} else {
console.log("The number is less than or equal to 10.");
}

What does the NULL data type represent in JavaScript?

View Answer:
Interview Response: nothingness or empty, but not equal undefined

Technical Response: It is a unique value that represents “nothing”, “empty” or “value unknown”. It is not a representation of a value that has not to be defined, non-existing, or a null pointer like other programming languages.

Code Example:

let myVariable = null;

console.log(myVariable); // Output: null
console.log(typeof myVariable); // Output: object

What does the undefined data type represent in JavaScript?

View Answer:
Interview Response: In JavaScript, the undefined data type represents a variable or object property that has been declared but not assigned a value, or a function that has no return value.

Code Example:

let age;

console.log(age); // shows "undefined"

// Technically, it is possible to explicitly assign undefined to a variable, but it is not recommended.

let age = 100;

// change the value to undefined

age = undefined;

console.log(age); // "undefined"

What is the difference between typeof x and typeof(x) with parentheses?

View Answer:
Interview Response: typeof x acts as an operator, and typeof(x) is a function, but they work with parentheses or without them. The result is the same.

What does the typeof operator return?

View Answer:
Interview Response: The typeof operator returns a string with the name of the type, like "number" if it is a number or “function” if it’s a function.

What are the primitive data types in JavaScript?

View Answer:
Interview Response: The primitive data types in JavaScript include the Number, String, Boolean, BigInt, Null, Undefined, and Symbol types.

What is the difference between null and undefined in JavaScript?

View Answer:
Interview Response: In JavaScript, null and undefined are two distinct data types that represent different types of non-values. undefined represents a value that has not been defined or has not yet been assigned a value, while null represents a deliberate absence of a value.

Code Example:

let myUndefinedVariable;
let myNullVariable = null;

console.log(myUndefinedVariable); // Output: undefined
console.log(myNullVariable); // Output: null

note

Note that typeof undefined returns "undefined", while typeof null returns "object", which is a quirk in the language's design that cannot be fixed for backward compatibility reasons.


What is NaN in JavaScript?

View Answer:
Interview Response: In JavaScript, NaN stands for "Not a Number" and is a special value that represents the result of an invalid or undefined mathematical operation.

Code Example:

let myNaN = 0 / 0;

console.log(myNaN); // Output: NaN
console.log(typeof myNaN); // Output: number
console.log(Number.isNaN(myNaN)); // Output: true

note

Note that typeof NaN returns "number", indicating that NaN is a numeric value, but Number.isNaN() is a method that can be used to check whether a value is equal to NaN.


What is a BigInt in JavaScript?

View Answer:
Interview Response: BigInt is a JavaScript numeric data type that can represent integers of arbitrary length, allowing for precise calculations beyond the limitations of the Number type.

Code Example:

let myBigInt = 12345678901234567890n;

console.log(myBigInt); // Output: 12345678901234567890n
console.log(typeof myBigInt); // Output: bigint

What is the difference between a primitive data type and an object data type in JavaScript?

View Answer:
Interview Response: A primitive is a basic data type that represents a single value, while an object is a complex structure that represents a collection of key-value pairs.

Here's an example of how to use a primitive data type:

let myNumber = 42;

console.log(myNumber); // Output: 42
console.log(typeof myNumber); // Output: number

Here's an example of how to use an object data type:

let myObject = {
name: "Alice",
age: 30,
isStudent: true
};

console.log(myObject); // Output: {name: "Alice", age: 30, isStudent: true}
console.log(typeof myObject); // Output: object

note

The main difference between primitive data types and object data types is that primitive data types are immutable (i.e., their values cannot be changed), while object data types are mutable (i.e., their values can be changed by adding, updating, or deleting key-value pairs). Additionally, primitive data types are passed by value, while object data types are passed by reference.