Datatypes in JS:
In JavaScript, a data type defines the type of value that a variable can hold.
The type determines what operations can be performed on that value.
JavaScript has both primitive data types (simple values) and reference data types (complex objects).
1. Primitive Data Types:
These are the most basic types in JavaScript, and they hold a single value.
They are immutable, meaning once their value is set, it cannot be changed.
a)Number:
Represents integers and floating-point numbers.
let x = 42;
b)String:
Represents a sequence of characters.
let name = "Alice";
c)Boolean:
Represents true or false.
let isActive = true;
d)Null:
Represents the intentional absence of any object value.
let person = null;
e)Undefined:
Indicates a variable has been declared but not assigned.
let value;
f)Symbol:
A unique identifier.
const uniqueID = Symbol();
g)BigInt:
For representing large integers.
let bigNum = 123n;
2. Reference Data Types:
These data types are more complex and store collections of values or more structured data.
They are mutable, meaning their content can be changed even if the reference remains the same.
a)Object:
A collection of key-value pairs.
let person = {name: "John"};
b)Array:
An ordered collection of values.
let arr = [1, 2, 3];
c)Function:
A block of code that performs a task.
function greet() {}
Variables in JS:
In JavaScript, a variable is a named container used to store data that can be referenced and manipulated throughout the code.
Variables allow you to store values such as numbers, strings, objects, and functions.
1. Declaring Variables:
In JavaScript, variables are declared using one of three keywords:
var (older way, less commonly used now)
let (block-scoped, introduced in ES6)
const (block-scoped, for constants, also introduced in ES6)
a) var (Old Way):
The var keyword was traditionally used to declare variables in JavaScript.
var name = "Alice";
var age = 25;
b) let (Modern Way):
Block scope means that the variable is only accessible within the block (e.g., inside an if statement or loop) where it is defined.
let name = "Bob";
let age = 30;
c) const (Constant Variables):
The const keyword is used to declare variables that should not be reassigned after their initial assignment.
const country = "USA";
Re-declaring Variables:
With var:
You can redeclare a variable within the same scope, and JavaScript will not throw an error.
var name = "Alice";
var name = "Bob"; // No error
console.log(name); // Output: Bob
With let and const:
You cannot redeclare a variable in the same scope. Doing so will result in a SyntaxError.
let name = "Alice";
let name = "Bob"; // SyntaxError: Identifier 'name' has already been declared
const country = "USA";
const country = "Canada"; // SyntaxError: Identifier 'country' has already been declared
Operators in JS:
In JavaScript, operators are symbols or keywords that perform operations on operands.
1. Arithmetic Operators:
These operators are used to perform mathematical operations on numeric values.
+ Addition 5 + 3 → 8
- Subtraction 5 - 3 → 2
* Multiplication 5 * 3 → 15
/ Division 5 / 3 → 1.6667
%**** Modulus (remainder of division) 5 % 3 → 2
** Exponentiation (power) 2 ** 3 → 8
++ Increment (adds 1) let x = 5; x++ → 6
-- Decrement (subtracts 1) let x = 5; x-- → 4
2. Assignment Operators:
These operators are used to assign values to variables.
= Simple assignment let x = 10;
+= Addition assignment x += 5; → x = x + 5;
-= Subtraction assignment x -= 5; → x = x - 5;
*= Multiplication assignment x *= 5; → x = x * 5;
/= Division assignment x /= 5; → x = x / 5;
%= Modulus assignment x %= 5; → x = x % 5;
**= Exponentiation assignment x **= 2; → x = x ** 2;
3. Comparison Operators:
These operators are used to compare two values and return a boolean (true or false).
== Equal to 5 == "5" → true
=== Strictly equal to 5 === "5" → false
!= Not equal to 5 != "5" → false
!== Strictly not equal to 5 !== "5" → true
> Greater than 5 > 3 → true
< Less than 5 < 3 → false
>= Greater than or equal to 5 >= 3 → true
<= Less than or equal to 5 <= 3 → false
4. Logical Operators:
These operators are used to perform logical operations, typically with boolean values.
&& Logical AND true && false → false
|| Logical OR true && false → true
! Logical NOT !true → false
Conditional Statements in JS:
In JavaScript, conditional statements are used to perform different actions based on whether a specific condition is true or false.
1. if Statement
The if statement is the most basic conditional statement. It executes a block of code if a specified condition evaluates to true.
Syntax:
if (condition) {
// Block of code to be executed if the condition is true
}
2. else Statement
The else statement follows an if statement and defines a block of code to be executed if the condition in the if statement is false.
Syntax:
if (condition) {
// Block of code to be executed if the condition is true
} else {
// Block of code to be executed if the condition is false
}
3. else if Statement
The else if statement allows you to test multiple conditions. It’s used when you need to check more than one condition.
Syntax:
if (condition1) {
// Block of code executed if condition1 is true
} else if (condition2) {
// Block of code executed if condition2 is true
} else {
// Block of code executed if neither condition1 nor condition2 is true
}
4. Nested if Statements
You can nest if statements inside other if statements to create more complex conditions.
5. Switch Statement
The switch statement is a more efficient way to test a value against multiple conditions, particularly when you have many different possible outcomes.
Syntax:
switch (expression) {
case value1:
// Code to run if expression === value1
break;
case value2:
// Code to run if expression === value2
break;
default:
// Code to run if no case matches
}
Looping in JS:
In JavaScript, loops are used to repeatedly execute a block of code as long as a specific condition is met.
This is useful when you want to perform repetitive tasks, like iterating over arrays or executing a code block multiple times.
1. for Loop
The for loop is the most common loop in JavaScript. It is typically used when you know how many times you want to iterate over a block of code.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to be executed in each iteration
}
2. while Loop
The while loop executes a block of code as long as the specified condition evaluates to true. The condition is checked before each iteration, and the loop will stop as soon as it becomes false.
Syntax:
while (condition) {
// Code to be executed as long as condition is true
}
Top comments (0)