Chapter 2: Basics of JavaScript
Tags: javascript
Published: 2023-05-30 12:57:11

## Syntax and Basic Constructs

JavaScript uses a syntax similar to other C-style languages (like C++ and Java). A JavaScript program is a sequence of statements and comments. Statements end with a semicolon (;), although they can be omitted due to Automatic Semicolon Insertion (ASI).

Here is a simple JavaScript statement:


```javascript
console.log('Hello, World!');
```

Comments can be used to make your code more understandable to others (or to yourself). JavaScript supports both single-line comments, started with `//`, and multi-line comments, enclosed between `/*` and `*/`.


```javascript
// This is a single-line comment

/* 
This is a 
multi-line comment 
*/
```

## Variables and Data Types

In JavaScript, we declare variables using the `var`, `let`, or `const` keywords. `var` is function-scoped, while `let` and `const` are block-scoped. `const` is used for variables that cannot be reassigned.


```javascript
var name = "John Doe";
let age = 21;
const country = "USA";
```

JavaScript has dynamic types, meaning a variable can hold different types of values because it’s not tied to any particular type. There are seven basic data types in JavaScript:

1.  **Number:** This includes both integers and floating-point numbers.
    
    
    ```javascript
    let count = 10; // integer
    let price = 9.99; // floating-point number
    ```
    
2.  **String:** A sequence of characters used to represent text.
    
    
    ```javascript
    let message = "Hello, World!";
    ```
    
3.  **Boolean:** Represents a logical entity and can have two values: `true` or `false`.
    
    
    ```javascript
    let isComplete = true;
    ```
    
4.  **Undefined:** A variable that has been declared but not yet assigned a value.
    
    
    ```javascript
    let x;
    console.log(x); // undefined
    ```
    
5.  **Null:** A special keyword representing a null or "empty" value.
    
    
    ```javascript
    let y = null;
    ```
    
6.  **Symbol:** A unique and immutable data type that is often used as an identifier for object properties.
    
7.  **Object:** Allows you to store collections of data.
    
    
    ```javascript
    let person = {
      name: "John Doe",
      age: 21
    };
    ```
    

## Operators and Expressions

Operators in JavaScript are used to perform operations on variables and values. The most common operators are:

-   **Arithmetic operators:** `+`, `-`, `*`, `/`, `%`, `++`, `--`
    
    
    ```javascript
    let x = 10;
    let y = 20;
    
    console.log(x + y); // 30
    ```
    
-   **Assignment operators:** `=`, `+=`, `-=`, `*=`, `/=`, `%=`
    
    
    ```javascript
    let x = 10;
    x += 5;
    
    console.log(x); // 15
    ```
    
-   **Comparison operators:** `==`, `===`, `!=`, `!==`, `<`, `>`, `<=`, `>=`
    
    
    ```javascript
    let x = 10;
    
    console.log(x == "10"); // true
    console.log(x === "10"); // false
    ```
    
-   **Logical operators:** `&&`, `||`, `!`
    
    
    ```javascript
    let x = 10;
    let y = 20;
    
    console.log(x > 5 && y > 5); // true
    ```
    

## Control Flow (If, Else, Switch, Loops)

Control flow statements are used to conditionally execute blocks of code.

-   **If-else statements**
    
    
    ```javascript
    let age = 21;
    
    if (age >= 18) {
      console.log("You are an adult.");
    } else {
      console.log("You are a minor.");
    }
    ```
    
-   **Switch statement**
    
    
    ```javascript
    let grade = 'A';
    
    switch (grade) {
      case 'A':
        console.log("Excellent");
        break;
      case 'B':
        console.log("Good");
        break;
      case 'C':
        console.log("Fair");
        break;
      case 'D':
        console.log("Poor");
        break;
      default:
        console.log("Invalid grade");
    }
    ```
    
-   **For loop**
    
    
    ```javascript
    for (let i = 0; i < 5; i++) {
      console.log(i); // 0, 1, 2, 3, 4
    }
    ```
    
-   **While loop**
    
    
    ```javascript
    let i = 0;
    
    while (i < 5) {
      console.log(i); // 0, 1, 2, 3, 4
      i++;
    }
    ```
    

By understanding these fundamental concepts, you're now ready to explore more complex aspects of the JavaScript language.