whenever life put's you in a tough situtation, never say why me! but, try me!

Chapter 2: Basic Syntax

Variables and Data Types

Variables

Variables are like containers that store data values. In JavaScript, you use variables to hold and manipulate information.

  • Declaration: You declare variables using var, let, or const.
    • var is the oldest way to declare variables and has some quirks.
    • let allows you to declare variables that can be reassigned.
    • const is used for variables that should not be reassigned after their initial value is set.

Example:

let age = 25; // A variable that can be changed
const name = "Alice"; // A variable that cannot be changed

Data Types

JavaScript has several types of data:

  1. Numbers: Represents both integers and floating-point numbers.

    • Example: let temperature = 22.5;
  2. Strings: Represents sequences of characters, enclosed in quotes.

    • Example: let greeting = "Hello, world!";
  3. Booleans: Represents true or false values.

    • Example: let isStudent = true;
  4. Null: Represents a deliberate non-value or empty value.

    • Example: let emptyValue = null;
  5. Undefined: Represents a variable that has been declared but not yet assigned a value.

    • Example: let notAssigned; // undefined
  6. Objects: Represents collections of related data and functions.

    • Example:
      let person = {
        name: "John",
        age: 30,
      };
      
  7. Arrays: Represents ordered lists of values.

    • Example:
      let colors = ["red", "green", "blue"];
      

Operators

Operators perform operations on variables and values.

  1. Arithmetic Operators: Perform mathematical operations.

    • + (Addition): 5 + 3 results in 8
    • - (Subtraction): 10 - 4 results in 6
    • * (Multiplication): 2 * 3 results in 6
    • / (Division): 8 / 2 results in 4
    • % (Modulus): 5 % 2 results in 1
  2. Comparison Operators: Compare values and return true or false.

    • == (Equal to): 5 == 5 is true
    • === (Strict equal to): 5 === '5' is false (type matters)
    • != (Not equal to): 5 != 4 is true
    • > (Greater than): 10 > 5 is true
    • < (Less than): 3 < 7 is true
  3. Logical Operators: Combine boolean values.

    • && (Logical AND): true && false is false
    • || (Logical OR): true || false is true
    • ! (Logical NOT): !true is false

Example:

let a = 10;
let b = 20;
let result = a + b; // result is 30

let isEqual = a == b; // isEqual is false
let isGreater = b > a; // isGreater is true

Control Flow

Control flow statements allow you to execute code based on certain conditions or repeatedly.

1. If Statement

The if statement allows you to execute a block of code only if a specified condition evaluates to true.

Syntax:

if (condition) {
  // code to execute if the condition is true
}

Example:

let temperature = 25;

if (temperature > 20) {
  console.log("It's warm outside.");
}

In this example, the message "It's warm outside." will be printed if temperature is greater than 20.

2. If/Else Statement

The if/else statement allows you to execute one block of code if the condition is true, and another block of code if the condition is false.

Syntax:

if (condition) {
  // code to execute if the condition is true
} else {
  // code to execute if the condition is false
}

Example:

let score = 75;

if (score >= 60) {
  console.log("You passed!");
} else {
  console.log("You failed.");
}

In this example, if score is 60 or higher, it will print "You passed!". If score is below 60, it will print "You failed."

3. Else If Statement

The else if statement allows you to check multiple conditions sequentially. It’s used when you have more than two conditions to test.

Syntax:

if (condition1) {
  // code to execute if condition1 is true
} else if (condition2) {
  // code to execute if condition2 is true
} else {
  // code to execute if none of the conditions are true
}

Example:

let age = 18;

if (age < 13) {
  console.log("You are a child.");
} else if (age < 20) {
  console.log("You are a teenager.");
} else {
  console.log("You are an adult.");
}

In this example:

  • If age is less than 13, it will print "You are a child."
  • If age is between 13 and 19 (inclusive of 13, but less than 20), it will print "You are a teenager."
  • If age is 20 or older, it will print "You are an adult."

4. Loops

Loops are used to execute a block of code multiple times.

  • For Loop: Executes a block of code a fixed number of times.

    Example:

    for (let i = 0; i < 5; i++) {
      console.log(i);
    }
    

    This loop prints numbers from 0 to 4.

  • While Loop: Executes a block of code as long as a condition is true.

    Example:

    let count = 0;
    while (count < 5) {
      console.log(count);
      count++;
    }
    

    This loop prints numbers from 0 to 4, incrementing count each time.

  • Do-While Loop: Similar to the while loop, but guarantees that the code block is executed at least once.

    Example:

    let num = 0;
    do {
      console.log(num);
      num++;
    } while (num < 5);
    

    This loop prints numbers from 0 to 4, incrementing num each time. It will run at least once, even if the condition is false initially.