JavaScript is one of the most popular programming languages in web development. It is mainly used to make websites interactive and dynamic. With JavaScript, we can create things like buttons, animations, calculators, login forms, games, and much more.
Variables
Variables used to store data in JavaScript.
JavaScript has 3 ways to create variables:
- var
- let
- const
var
var has global scope and it is the old way of declaring variables.
var a = 10;
console.log(a);
output:
10
let
let has block scope. It can only be accessed inside { }.
Example:
let a = 10;
{
let a = 20;
console.log(a); // 20
}
console.log(a); // 10
OutPut :
10
const
const is used for constant values. Its value cannot be reassigned.
const c = 30;
console.log(c);
OutPut:
30 ā
const weekdays = 7;
weekdays = 10; ā Error
OutPut:
TypeError: Assignment to constant variable
But if the value is an array or object, we can change values inside it.š
Data Types
JavaScript has different data types.
1.Primitive Data Types
These store single values.
- Number
- String
- Boolean
- Null
- Undefined
Example:
- let age = 20;
- let name = "Surya";
- let Student = true;
- let salary =null;
- let a
2.Non-Primitive Data Types
These can store multiple values.
- Array
- Object
Operators
Operators are symbols that help us perform calculations, comparisons, and logical operations in JavaScript.
1. Arithmetic Operators
Operator Meaning
- { + } Add
- { - } Subtract
- { * } Multiply
- { / } Divide
- { % } Remainder
Example:
let a = 10;
let b = 5;
console.log(a + b);
output:
15
console.log(a - b);
output:
5
2.Comparison Operators
*comparison of Two values *
Operator Meaning
- { == } Equal
- { === } Strict Equal
- { != } Not Equal
- { > } Greater Than
- { < } Less Than
Example:
let a = 10;
let b = 5;
console.log(a > b);
Output:
true
3.Logical Operators
Logical operators are used to combine two or more conditions.
Operator Meaning
- { && } AND ā both conditions must be true
- { || } OR ā any one condition can be true
- { ! } NOT ā changes true to false and false to true
Example:
Use &&
let age = 20;
console.log(age > 18 && age < 30);
Output:
true
let age = 20;
console.log(age < 18 && age < 30);
Output:
false
Use ||
let age = 20;
console.log(age > 18 || age < 30);
Output:
true
let age = 20;
console.log(age < 18 || age < 30);
Output:
true
Increment Operator ++
Used to increase Value.
Two Type of increment operators
- ++a--- Pre-increment
- a++--- Post-increment
Example:
let a = 5;
a++;
console.log(a);
Output:
6
let a = 5;
let b = a;
a++;
console.log(a);
console.log(b);
Output:
6
5
Decrement Operator --
Used to decrease value
Two Type of Decrement operators
- --a ---Pre-decrement
- a-- ---Post-decrement
Example:
let a = 5;
a--;
console.log(a);
Output:
4
Arrays
Array is used to Store Multiple Values in a Single Variable [].
Example:
const marks = [79, 70, 93, 100];
index: 0 1 2 3
console.log(marks[1]);
Output:
70
Multiple Data Types in One Array
Arrays can store different data types together.
const values = [10, true, "abc", 50];
Changing Array Values with const
Call the index Num when Change the Value.
When using const, you cannot reassign the whole array.
But you can change values inside the array.
const marks = [10, 20, 0, 11, 5];
marks[2] = 99;
console.log(marks);
Functions
A function is a reusable block of code.
It runs only when the function is called.
Function Declaration
function add(no1, no2) {
}
add(5, 10);
Arrow Function
Arrow function is a shorter way to write functions.
const add = (no1, no2) => no1 + no2;
add(10, 15);
Return Value
The return keyword sends a value back from the function.
function add(no1, no2) {
return no1 + no2;
}
let result = add(5, 7);
console.log(result);
OutPut:
12
Hoisting
Hoisting means JavaScript moves declarations to the top before running the code.
-Functions are hoisted ā
-let and const variables are not hoisted ā
let result = add(5, 7);
console.log(result);
function add(no1, no2) {
return no1 + no2;
}
Objects
An object stores data in key : value pairs.
const laptop = {
brand: "HP",
price: 35000,
model: "HP05"
};
console.log(laptop.brand);
console.log(laptop.price);
Objects with Functions
Objects can also contain functions.
const obj = {
type: "object",
state: "active",
behavior: function() {
console.log("Object is working");
}
};
obj.behavior();
Top comments (1)
You've missed two primitive types: Symbol and BigInt