Skip to content

Latest commit

 

History

History
364 lines (244 loc) · 8.77 KB

File metadata and controls

364 lines (244 loc) · 8.77 KB

01 Data Types

JavaScript has eight data types, which are divided into two main groups:

  • Primitive Data Types
  • Non-Primitive Data Types

Note: NaN is not a data type. It is a special numeric value.

01.1 Primitive Data Types

Primitive values are stored directly in memory and are not stored as references to objects.

Numeric Type

  • Number
  • BigInt

Non-Numeric Type

  • String
  • Boolean
  • Null
  • Undefined
  • Symbol

01.1.1 Number

The Number data type represents both integers and floating-point values. JavaScript numbers are based on the 64-bit floating-point format (IEEE 754).

let age = 25;
let height = 5.8;

console.log(age); // 25
console.log(height); // 5.8
Console Log

console.log() is used to display output in the console. It is mainly used for debugging and checking values while writing JavaScript code.

let name = "Jagan";
console.log(name); // Jagan
Safe Limit

The safe limit is the range of whole numbers that the Number data type can represent accurately (without losing precision).

  • Maximum safe integer: Number.MAX_SAFE_INTEGER → 9,007,199,254,740,991
  • Minimum safe integer: Number.MIN_SAFE_INTEGER → −9,007,199,254,740,991
console.log(Number.MAX_SAFE_INTEGER); // 9,007,199,254,740,991
console.log(Number.MIN_SAFE_INTEGER); // −9,007,199,254,740,991

01.1.2 BigInt

BigInt is used to represent very large whole numbers that are greater than the safe limit of the Number type.

let bigOne = 12345678901234567890n;
let bigTwo = BigInt("12345678901234567890");

console.log(bigOne); // 12345678901234567890
console.log(bigTwo); // 12345678901234567890

Note: BigInt cannot contain decimal values.

01.1.3 String

The String data type is used to store text. Strings can be created using single quotes, double quotes, or template literals.

let firstName = "Jagan"; // Double Quotes
let lastName = 'Ganesh'; // Single Quotes

console.log(firstName); // Jagan
console.log(lastName); // Ganesh
Template literals

Template literals allow you to embed variables, expressions, and multi-line text inside strings.

let name = "Jagan";
let age = 32;

let message = `My name is ${name} and I am ${age} years old.`;
console.log(message); // My name is Jagan and I am 32 years old.

01.1.3.1 Characters in a String

A string is made up of individual characters.

Each character in a string has a position, called an index.
In JavaScript, the index starts from 0.

let name = "Jagan";

console.log(name[0]); // J
console.log(name[1]); // a
console.log(name[2]); // g

Note: Spaces and special symbols are also counted as characters in a string.

01.1.3.2 Find Length of a String

You can find the number of characters in a string using the .length property.

let city = "Chennai";
console.log(city.length); // 7

01.1.3.3 Bracket Notation in Strings

Bracket notation allows you to access individual characters in a string using their index.

// Access First Character
let name = "John";
console.log(name[0]); // "J"

// Access Last Character
let name = "Mike";
console.log(name[name.length - 1]); // "e"
String Immutability

Strings are immutable in JavaScript, which means once a string is created, you cannot change its individual characters directly. If you want a different string, you have to create a new string.

let lastName = "Doe";

// Trying to change a character does NOT work
lastName[0] = "J";
console.log(lastName); // "Doe" (remains unchanged)

// Creating a new string works
lastName = "Joe";
console.log(lastName); // "Joe"

01.1.3.4 Concatenate Strings

Strings can be concatenated using the + operator, compound assignment, or template literals.

01.1.3.5 Escape Sequence in Strings

Escape sequences are used to represent special characters inside strings.

\' → single quote
\" → double quote
\\ → backslash
\n → new line
\t → tab
\r → carriage return (moves the cursor to the beginning of the line; originally used in typewriters)
\b → backspace
\f → form feed (originally used to advance to the next page in printing; now rarely used and implementation-dependent)

01.1.4 Boolean

A Boolean data type has only two possible values:

  • true
  • false
let isLoggedIn = true;
let hasAdminAccess = false;

console.log(isLoggedIn); // true
console.log(hasAdminAccess); // false

01.1.5 Null

null is used to represent no value or an empty value.
It is used when you want to intentionally assign nothing to a variable.

let currentUser = null;

console.log(currentUser); // null
console.log(typeof currentUser); // object (this is a known JavaScript quirk)

01.1.6 Undefined

undefined means a variable has been declared but has not been assigned a value.

let name;

console.log(name); // undefined
console.log(typeof name); // undefined

Null → "Nothing here, I put it intentionally" Think of an empty box you placed on purpose.

Undefined → "I forgot to put something here" Think of a box that exists but you never filled.

01.1.7 Symbol

Symbol is a unique and immutable primitive data type. Symbols are mainly used as unique identifiers for object properties, ensuring that property keys do not conflict with others.

let id1 = Symbol("id");
let id2 = Symbol("id");

console.log(id1 === id2); // false (each Symbol is unique)

Note: Symbols are perfect when you need unique, collision-free identifiers — for object keys, constants, or customizing behavior with well-known symbols.

Example Scenario
// Create unique symbols for different roles
const admin = Symbol("role");
const editor = Symbol("role");

let user = {};
user[admin] = "Alice";
user[editor] = "Bob";

console.log(user[admin]); // Alice
console.log(user[editor]); // Bob
console.log(admin === editor); // false (each Symbol is unique)

Note: Even though both symbols have the same description "role", they are unique identifiers.

01.2 Non-Primitive Data Types

Non-Primitive data types, also known as reference types or object types, are used to store collections of data and more complex entities.

Note: Unlike primitive types (which store the actual value), non-primitive variables store a reference to the location of the object in memory.

All non-primitive values in JavaScript are objects.

  • Object
  • Date
  • RegExp
  • Array
  • Function

01.2.1 Object

An object is used to store data in the form of key-value pairs.

let user = {
  name: "Jagan",
  age: 25,
  isActive: true,
};

console.log(user.name); // Jagan
console.log(user.age); // 25

Note: Objects are mutable, which means their values can be changed.

01.2.2 Array

Array values are stored in an ordered list, and each value has an index number (starting from 0).

let fruits = ["Apple", "Banana", "Orange"];

console.log(fruits[0]); // Apple
console.log(fruits[2]); // Orange

Find Length of an Array

console.log(fruits.length); // 3

Note: Arrays are mutable, meaning elements can be added, removed, or changed.

01.2.3 Function

A function performs a specific task and runs only when it is called.

const greet = (name) => {
  console.log("Hello " + name);
};

greet("Jagan"); // Hello Jagan

01.2.3.1 Passing values to a Function with Arguments

const myFunc = (isActive) => {
  console.log("Argument: " + isActive);
};

01.2.3.2 Function with Return Value

const add = (a, b) => {
  return a + b;
};

console.log(add(5, 10)); // 15

Note: If a function does not explicitly return a value, it automatically returns undefined.

01.2.4 Date

The Date object is used to work with dates and times in JavaScript. It allows you to create, read, and manipulate dates such as the current date, specific dates, and time values.

let today = new Date();

console.log(today); // current date and time
Get Date Components
let now = new Date();

console.log(now.getFullYear()); // Year
console.log(now.getMonth()); // Month (0–11)
console.log(now.getDate()); // Day of the month
console.log(now.getHours()); // Hours
console.log(now.getMinutes()); // Minutes

Note: The Date object is mutable, meaning its values can be changed using setter methods like setFullYear(), setMonth(), etc.

01.2.5 RegExp

The RegExp (Regular Expression) is used to search, match, and manipulate patterns in strings. It is especially useful for validation, searching, and text replacement.

let pattern = /hello/;
let text = "hello world";

console.log(pattern.test(text)); // true