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 TypeExample
Numberlet a=5;
Stringlet name="Geetha";
Booleanlet isStudent=true;
Nulllet value=null;
Undefinedlet a;
Symbollet id=Symbol("id");
BigIntlet big= 99999999999999999;

Reference Data Types

Data TypeExample
Objectlet names={name:"Geetha",age:22};
Arraylet names=["Geetha","Haveela","Rishitha"];
Typeoftypeof "name" //Returns "string"

Operators in JavaScript

Arithmetic Operators (Math operations)
OperatorDescriptionExample
+Addition5+2 // 7
-Subtraction5-2 // 3
*Multiplication5×2 //10
/Division10/2 //5
%Modulus (Remainder)10%3 // 1
**Exponentiation2**3 // 8
Comparison Operators (Returns true or false)
OperatorDescriptionExample
\==qual to5 == "5" // true
\===Strict equal (checks type & value)5 === "5" // false
Not equal5 != 3 // true
≠=Strict not equal5 !== "5" // true
\>;Greater than10 > 5 // true
<Less than10 < 5 // false
Logical Operators
OperatorDescriptionExample
&&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!");
}