Data Types - JavaScript Interview Questions
JavaScript Fundamentals: JavaScript Data Types
In JavaScript, how many data types are there?
View Answer:
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:
JavaScript supports two forms of syntax:
- As an operator: typeof x.
- As a function: typeof(x).
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:
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:
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:
Which are the three types of quotes that can be used to create a string representation of a value in JavaScript?
View Answer:
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:
- 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!
- 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.
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:
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:
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:
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:
What does the typeof operator return?
View Answer:
What are the primitive data types in JavaScript?
View Answer:
What is the difference between null and undefined in JavaScript?
View Answer:
let myUndefinedVariable;
let myNullVariable = null;
console.log(myUndefinedVariable); // Output: undefined
console.log(myNullVariable); // Output: null
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:
let myNaN = 0 / 0;
console.log(myNaN); // Output: NaN
console.log(typeof myNaN); // Output: number
console.log(Number.isNaN(myNaN)); // Output: true
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:
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:
let myNumber = 42;
console.log(myNumber); // Output: 42
console.log(typeof myNumber); // Output: number
let myObject = {
name: "Alice",
age: 30,
isStudent: true
};
console.log(myObject); // Output: {name: "Alice", age: 30, isStudent: true}
console.log(typeof myObject); // Output: object
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.