0% found this document useful (0 votes)
6 views10 pages

Web Notes - Data Types in JavaScript

The document provides an overview of data types in JavaScript, categorizing them into primitive (undefined, null, boolean, number, string, symbol, bigint) and complex (object) types. It explains the characteristics of each type, including their behavior and how to check their types using the 'typeof' operator. Additionally, it includes interview questions and answers related to JavaScript data types.

Uploaded by

ashish63700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Web Notes - Data Types in JavaScript

The document provides an overview of data types in JavaScript, categorizing them into primitive (undefined, null, boolean, number, string, symbol, bigint) and complex (object) types. It explains the characteristics of each type, including their behavior and how to check their types using the 'typeof' operator. Additionally, it includes interview questions and answers related to JavaScript data types.

Uploaded by

ashish63700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Web Notes - Data Types in JavaScript

Instructor - Pramod Kumar Jena

JavaScript Data Types – Notes

1. Definition
A data type defines the kind of value a variable can hold.

●​ In JavaScript, every value belongs to a type.​

●​ Data types help the interpreter decide how the value will behave in operations.​

👉 JavaScript is a dynamically typed language → variables are not tied to a fixed type, and
their type can change at runtime.

2. Categories of Data Types


JavaScript has:

1.​ Primitive Data Types (simple, immutable values):​

○​ undefined​

○​ null​

○​ boolean​

○​ number​

○​ string​

○​ symbol (ES6)​
○​ bigint (ES2020)​

2.​ Complex Data Type:​

○​ object (collection of properties)​

3. Primitive Data Types


a) Undefined

●​ A variable declared but not assigned any value is undefined.​

●​ typeof returns "undefined".​

let a;
console.log(a); // undefined
console.log(typeof a); // "undefined"

👉 Real-life analogy: A labeled empty box.


b) Null

●​ Represents intentional absence of any object value.​

●​ typeof null returns "object" (a known bug in JavaScript).​

●​ Loose equality: null == undefined → true.​

let obj = null;


console.log(obj); // null
console.log(typeof obj); // object

👉 Real-life analogy: An empty glass reserved for water (intentionally empty).

c) Boolean

●​ Only two values: true or false.​

●​ Used for logical conditions.​

let isLoggedIn = true;


let isPaid = false;

🔎 Conversion Rules (using Boolean()):


●​ "", 0, NaN, null, undefined → false.​

●​ Non-empty string, non-zero numbers, objects → true.​

👉 Real-life analogy: A switch (ON = true, OFF = false).

d) Number
●​ Represents both integers and floating-point numbers.​

let age = 25; // integer


let price = 19.99; // floating-point

●​ Special numeric values:​

○​ Infinity, -Infinity​

○​ NaN (Not a Number)

○​ The NaN has two special characteristics:

○​ Any operation with NaN returns NaN.


○​ The NaN does not equal any value, including itself.

Here are some examples:

○​ console.log(NaN/2); // NaN
○​ console.log(NaN == NaN); // false

console.log(10/0); // Infinity
console.log("a"/2); // NaN

👉 Real-life analogy: A calculator display (can show numbers, infinity, or error).

e) String

●​ In JavaScript, a string is a sequence of zero or more characters. A string literal begins


and ends with either a single quote(') or a double quote (").​

●​ Strings are immutable (cannot be changed directly).​


let name = "Pramod";
let message = 'Hello World';
let message = 'I\'m also a valid string'; // use \ to escape the
single quote(')
let text = "I'm learning JS"; // escape not needed in double quotes

🔎 Concatenation creates a new string:


let str = "Java";
str = str + "Script"; // "JavaScript"

👉 Real-life analogy: A sentence written on paper – to change it, you rewrite it.

f) Symbol (ES6)

●​ Represents a unique value (useful for object property keys).​

●​ Always unique, even if they have the same description.​

let s1 = Symbol("id");
let s2 = Symbol("id");
console.log(s1 == s2); // false

👉 Real-life analogy: A locker key – each key is unique.

g) BigInt (ES2020)

●​ Used for very large integers (beyond Number.MAX_SAFE_INTEGER = 2^53 - 1).​

●​ Created by appending n.​

let bigNum = 123456789012345678901234567890n;


console.log(typeof bigNum); // bigint
👉 Real-life analogy: Counting stars in the galaxy – numbers too big for normal counting.

4. Complex Data Type


a) Object

●​ A collection of key-value pairs.​

●​ Keys (properties) are strings (or symbols), values can be any type.​

let person = {
firstName: "John",
lastName: "Doe",
age: 30,
address: {
city: "Bhubaneswar",
country: "India"
}
};
console.log(person.firstName); // John
console.log(person["age"]); // 30

👉 Real-life analogy: A dictionary → word (key) + meaning (value).

5. typeof Operator
Used to check the type of a value:

console.log(typeof 123); // "number"


console.log(typeof "Hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (bug)
console.log(typeof Symbol()); // "symbol"
console.log(typeof 10n); // "bigint"
console.log(typeof {}); // "object"

6. Summary
●​ Primitive types: undefined, null, boolean, number, string, symbol, bigint.​

●​ Complex type: object.​

●​ JavaScript is dynamically typed → variable types can change at runtime.​

●​ Use typeof to check a value’s type.​

●​ Special values: NaN, Infinity, null.​

●​ Strings are immutable.​

●​ null and undefined are different but loosely equal.​

7. Interview Questions 🎯
1.​ List all data types in JavaScript.​

2.​ What is the difference between null and undefined?​

3.​ Why does typeof null return "object"?​

4.​ Explain NaN. Why is NaN == NaN false?​

5.​ What is the difference between primitive and object data types?​

6.​ What is BigInt? When should we use it?​

7.​ What is the use of Symbols in JavaScript?​

8.​ Are strings mutable in JavaScript? Explain with example.​


👉 These notes are detailed enough for semester study and cover interview-level
concepts (like typeof null, NaN, BigInt, Symbol).

Question 1
Which of the following is not a primitive data type in JavaScript?
●​ null
●​ undefined
●​ object
●​ symbol

Question 2
What will be the output of the following code: console.log(typeof(null));?
●​ null
●​ undefined
●​ object
●​ string

Question 3
Which operator is used to determine the type of a variable in JavaScript?
●​ typeof
●​ instanceof
●​ type
●​ is

Question 4
What is the value of a variable that has been declared but not initialized?
●​ null
●​ 0
●​ undefined
●​ empty string

Question 5
Which of the following is a valid way to declare a bigint in JavaScript?
●​ let num = 123n;
●​ let num = BigInt(123);
●​ let num = 123;
●​ Both A and B

Question 6
What will be the output of the following code: console.log(NaN == NaN);?
●​ true
●​ false
●​ undefined
●​ NaN

Question 7
Which of the following is a characteristic of JavaScript strings?
●​ Mutable
●​ Immutable
●​ Can contain numbers
●​ Can contain objects

Question 8
What will be the output of the following code: console.log(Boolean(''));?
●​ true
●​ false
●​ undefined
●​ null

Question 9
Which of the following is a correct way to create a symbol in JavaScript?
●​ let sym = Symbol();
●​ let sym = new Symbol();
●​ let sym = Symbol('description');
●​ Both A and C

Question 10
What will be the output of the following code: console.log(typeof 123n);?
●​ number
●​ bigint
●​ string
●​ undefined

You might also like