DEV Community

shunku
shunku

Posted on

Chapter 2: Basics of JavaScript

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:

console.log('Hello, World!');
Enter fullscreen mode Exit fullscreen mode

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 */.

// This is a single-line comment

/* 
This is a 
multi-line comment 
*/
Enter fullscreen mode Exit fullscreen mode

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.

var name = "John Doe";
let age = 21;
const country = "USA";
Enter fullscreen mode Exit fullscreen mode

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.

    let count = 10; // integer
    let price = 9.99; // floating-point number
    
  2. String: A sequence of characters used to represent text.

    let message = "Hello, World!";
    
  3. Boolean: Represents a logical entity and can have two values: true or false.

    let isComplete = true;
    
  4. Undefined: A variable that has been declared but not yet assigned a value.

    let x;
    console.log(x); // undefined
    
  5. Null: A special keyword representing a null or "empty" value.

    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.

    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: +, -, *, /, %, ++, --

    let x = 10;
    let y = 20;
    
    console.log(x + y); // 30
    
  • Assignment operators: =, +=, -=, *=, /=, %=

    let x = 10;
    x += 5;
    
    console.log(x); // 15
    
  • Comparison operators: ==, ===, !=, !==, <, >, <=, >=

    let x = 10;
    
    console.log(x == "10"); // true
    console.log(x === "10"); // false
    
  • Logical operators: &&, ||, !

    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

    let age = 21;
    
    if (age >= 18) {
      console.log("You are an adult.");
    } else {
      console.log("You are a minor.");
    }
    
  • Switch statement

    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

    for (let i = 0; i < 5; i++) {
      console.log(i); // 0, 1, 2, 3, 4
    }
    
  • While loop

    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.

Top comments (0)