Javascript
Module 2
Data types in JS
● JavaScript utilizes dynamic typing, meaning variable types are determined at runtime rather
than compile-time. There are eight data types in JavaScript, categorized as primitive and
non-primitive (object).
○ Primitive Data types :
■ These are the most basic building blocks and represent single, immutable values.
■ Ensuring efficiency in both memory usage and performance.
○ Non primitive Data types
■ The Object type is the only non-primitive data type, used to store collections of data
and more complex structures.
■ A collection of key-value pairs (properties and methods) enclosed in curly braces
({}). Other non-primitive types like Arrays ([]) and Functions are technically a
special kind of object.
Data Types
Primitive Non primitive
String Number BigInt Object
Boolean
Symbol
Null
Primitive Data types
● String: Represents textual data enclosed in single quotes (' '), double quotes (" "), or backticks (`).
○ Example: let name = "John Doe";
● Number: Represents both integer and floating-point numbers, including special values like Infinity, -Infinity, and NaN
(Not-a-Number). JavaScript numbers are always 64-bit floating-point values internally.
○ Example: let age = 25; let price = 99.99;
● BigInt: A newer data type (introduced in ES2020) that can represent integers of arbitrary magnitude, beyond the safe limit of
the regular Number type. They are created by appending n to the end of an integer literal.
○ Example: let largeNum = 9007199254740991n;
● Boolean: Represents a logical entity with only two possible values: true or false. This is commonly used for conditional
logic.
○ Example: let isLoggedIn = true;
● Undefined: Automatically assigned as the default value to variables that have been declared but not assigned a value.
○ Example: let value; // value is undefined
● Null: A special value that represents the intentional absence of any object value. It is explicitly assigned by a developer to
indicate an empty or unknown value.
○ Example: let data = null;
● Symbol: A unique and immutable primitive value, often used as an identifier for object properties to prevent naming conflicts.
○ Example: let id = Symbol("uniqueId");
Non primitive Data type
The Object type is the only non-primitive data type, used to store collections of data and more
complex structures.
● Object: A collection of key-value pairs (properties and methods) enclosed in curly braces ({}).
Other non-primitive types like Arrays ([]) and Functions are technically a special kind of object.
○ Example (Object Literal): let person = { name: "Alice", age: 30 };
○ Example (Array): let colors = ["red", "green", "blue"];
○ Example (Function): function add(a, b) { return a + b; };
● The typeof operator returns the type of the operand. It’s useful when we want to process values
of different types differently or just want to do a quick check.
Arrays
● Objects allow you to store keyed collections of values
● Special data structure named Array, to store ordered collections
● There are two syntaxes for creating an empty array:
Array : Queues and Stacks
● Queues in JavaScript are linear data structures that adhere to the First-In, First-Out (FIFO) principle. This means the
first element added to the queue is the first one to be removed.
○ Supports two operations:
Enqueue appends an element to the end.
Dequeue get an element from the beginning, advancing the queue, so that the 2nd element becomes
the 1st.
● Stacks, the latest pushed item is received first, that’s also called LIFO (Last-In-First-Out) principle.
○ It supports two operations:
push adds an element to the end.
pop takes an element from the end.
Decisions and Loops
● DECISION MAKING (Conditional Statements)
○ Decision-making allows the program to choose one path out of many based on a condition.
■ a) if statement
● Executes a block of code only if the condition is true.
Decisions and Loops
● DECISION MAKING (Conditional Statements)
○ Decision-making allows the program to choose one path out of many based on a condition.
■ if–else statement
● Two possible paths: one if condition is true, another if false.
Decisions and Loops
● DECISION MAKING (Conditional Statements)
○ Decision-making allows the program to choose one path out of many based on a condition.
■ else-if / elif ladder
● When you have multiple conditions.
Decisions and Loops
● Loops in JavaScript are used to reduce repetitive tasks by repeatedly executing a block of code as
long as a specified condition is true. This makes code more concise and efficient.
● In JavaScript, there are three types of Loops :
○ for Loop
○ while Loop
○ do-while Loop
Loop structure
● Initialization condition: Here, we initialize the variable in use. It marks the start of a for loop. An
already declared variable can be used or a variable can be declared, local to loop only.
● Testing Condition: It is used for testing the exit condition for a loop. It must return a boolean value. It
is also an Entry Control Loop as the condition is checked prior to the execution of the loop
statements.
● Statement execution: Once the condition is evaluated to true, the statements in the loop body are
executed.
● Increment/ Decrement: It is used for updating the variable for next iteration.
● Loop termination: When the condition becomes false, the loop terminates marking the end of its life
cycle.
Loops
The for loop repeats a block of code a
specific number of times. It contains
initialization, condition, and
increment/decrement in one line.
This JavaScript program for-in loop
iterates over the keys of the arr object and
prints each key with its corresponding
value.
The forEach loop executes a provided
function once for each element in an
array. The below JavaScript program
forEach loop executes a function for
each element in the numbers array,
printing each number.
Loop : while loop
The while loop executes as long as the
condition is true. It can be thought of as a
repeating if statement.
The below JavaScript program while loop prints
"Number:" followed by i repeatedly while i is
less than 3, incrementing i by 1 each time.
Loop :do while loop
The do-while loop is similar to while loop except it executes the code block at least once
before checking the condition.
The below JavaScript program do-while loop prints "Iteration:" followed by i,
increments i by 1, and repeats the process while i is less than 3, ensuring the block runs
at least once.
Output of code ???