Day 1 of JavaScript
What is JavaScript?
JavaScript (JS) is a scripting language used to make web pages interactive. It runs in the browser and allows developers to create dynamic content like pop-ups, animations, and form validation.
Variables in JavaScript
JavaScript Variables can be declared in 4 ways:
var
let
const
Var
var is used to declare variables, we can also redeclare and update using var
var name = "Geetha";
var name = "Haveela";
console.log(name); //Output: Haveela
Let
let is used to declare variables, we can update variables using let but we can’t redeclare them.
let i=22;
i=30;
console.log(i); //Output: 30
Const
Const is used to declare variables, here we can not update or redeclare varibles.
const name="Geetha";
name="Haveela"; //we will get an error message
Data Types in JavaScript
JavaScript has two main types of data:
Primitive Data Types
Data Type | Example |
Number | let a=5; |
String | let name="Geetha"; |
Boolean | let isStudent=true; |
Null | let value=null; |
Undefined | let a; |
Symbol | let id=Symbol("id"); |
BigInt | let big= 99999999999999999; |
Reference Data Types
Data Type | Example |
Object | let names={name:"Geetha",age:22}; |
Array | let names=["Geetha","Haveela","Rishitha"]; |
Typeof | typeof "name" //Returns "string" |
Operators in JavaScript
Arithmetic Operators (Math operations)
Operator | Description | Example |
+ | Addition | 5+2 // 7 |
- | Subtraction | 5-2 // 3 |
* | Multiplication | 5×2 //10 |
/ | Division | 10/2 //5 |
% | Modulus (Remainder) | 10%3 // 1 |
** | Exponentiation | 2**3 // 8 |
Comparison Operators (Returns true or false)
Operator | Description | Example |
\== | qual to | 5 == "5" // true |
\=== | Strict equal (checks type & value) | 5 === "5" // false |
≠ | Not equal | 5 != 3 // true |
≠= | Strict not equal | 5 !== "5" // true |
\>; | Greater than | 10 > 5 // true |
< | Less than | 10 < 5 // false |
Logical Operators
Operator | Description | Example |
&& | AND | (5 > 3 && 10 > 5) // true |
! | NOT | !(5 > 3) // false |
Control Flow
if-else
let age = 18;
if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are a minor");
}
switch
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the week!");
break;
case "Friday":
console.log("Weekend is near!");
break;
default:
console.log("Just another day!");
}