Comment Code


There are two ways to write comments in JavaScript:

Using // will tell JavaScript to ignore the remainder of the text on the current line. This is an in-line comment:

// This is an in-line comment.

You can make a multi-line comment beginning with /* and ending with */. This is a multi-line comment:

/* This is a
multi-line comment */

Variables


Variables are used to store data values. JavaScript variables can hold any data type, and their values can be changed during runtime. In this guide, we'll cover everything you need to know about variables in JavaScript.

Variable Declaration

In JavaScript, variables are declared using the let, const, or var keyword, followed by the variable name. For example:

let x;
const y = 10;
var z = "hello";

Variable Naming

In JavaScript, variable names must begin with a letter, underscore (_), or dollar sign ($). After the first character, variable names can also contain numbers. Variable names are case-sensitive, so myVar and myvar are two different variables.

It's important to choose meaningful variable names that describe the data they store. For example:

let firstName = "John";
const age = 30;
var isStudent = true;

Variable Assignment

Variables in JavaScript can be assigned a value using the assignment operator (=). For example:

let x;
x = 5;

Variables can also be declared and assigned a value in the same statement:

let y = 10;
const message = "Hello, world!";
var z = true;

Multiple variables can be declared and assigned values in a single statement, using commas to separate them:

let a = 1, b = 2, c = 3;

Data Types


Data Type Description Example
Number Represents numerical values, including integers and floating-point numbers. 3, 3.14, 0xff
String Represents textual data. Strings are enclosed in quotes (single or double). "hello", 'world', "123"
Boolean Represents a logical value, either true or false. true, false
Undefined Represents the absence of a value or an uninitialized variable. let x;
Null Represents the intentional absence of any object value. null
Symbol Represents a unique identifier. Symbols are immutable and are often used as keys in objects. Symbol('foo')
Object Represents a collection of properties, where each property has a name and a value.
Objects can be created using object literals, constructor functions, or the Object() constructor. {name: 'John', age: 30}, new Date()
Array A type of object that represents an ordered collection of values, where each value can be accessed using its index.
Arrays can be created using array literals or the Array() constructor. [1, 2, 3], new Array(4)
Function Represents a reusable block of code that performs a specific task. Functions can be created using function declarations, function expressions, or arrow functions. function add(a, b) { return a + b; }, () => console.log('Hello, world!')

Operators


JavaScript operators are symbols used to perform operations on values and variables. They allow you to manipulate data, compare values, and perform mathematical operations, among other things. Here's an overview of the different types of operators in JavaScript:

Arithmetic Operators

Arithmetic operators perform basic mathematical operations:

let a = 10;
let b = 3;

console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.333...
console.log(a % b); // 1

Comparison Operators

Comparison operators compare two values and return a boolean result (true or false):

let x = 5;
let y = "5";

console.log(x == y);  // true
console.log(x === y); // false
console.log(x != y);  // false
console.log(x !== y); // true

Logical Operators

Logical operators are used to combine multiple conditions:

let a = true;
let b = false;

console.log(a && b); // false
console.log(a || b); // true
console.log(!a);     // false

Assignment Operators

Assignment operators are used to assign values to variables:

let x = 10;

x += 5; // x = x + 5; -> x = 15
x -= 3; // x = x - 3; -> x = 12
x *= 2; // x = x * 2; -> x = 24
x /= 4; // x = x / 4; -> x = 6
x %= 5; // x = x % 5; -> x = 1

String Operators

The + operator can be used to concatenate strings:

let firstName = "John";
let lastName = "Doe";

console.log(firstName + " " + lastName); // "John Doe"

The += operator can also be used for string concatenation:

let fullName = "John";
fullName += " Doe";

console.log(fullName); // "John Doe"

Control Structures


Control structures are essential elements in programming languages that allow you to control the flow of your code based on certain conditions or to perform repetitive tasks. In JavaScript, there are several control structures you should be familiar with:

If Statement

The if statement is used to execute a block of code if a specified condition is true:

let age = 18;

if (age >= 18) {
  console.log("You are an adult.");
}

If-Else Statement

The if-else statement is used to execute one block of code if a condition is true and another block of code if the condition is false:

let age = 16;

if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are not an adult.");
}

else-If Statement

The else-If statement is used to test multiple conditions and execute different blocks of code based on the first true condition:

let grade = 85;

if (grade >= 90) {
  console.log("You got an A.");
} else if (grade >= 80) {
  console.log("You got a B.");
} else if (grade >= 70) {
  console.log("You got a C.");
} else {
  console.log("You did not pass.");
}

Switch Statement

The switch statement is used to perform different actions based on the value of a variable or expression:

let day = 3;

switch (day) {
  case 0:
    console.log("Sunday");
    break;
  case 1:
    console.log("Monday");
    break;
  case 2:
    console.log("Tuesday");
    break;
  case 3:
    console.log("Wednesday");
    break;
  case 4:
    console.log("Thursday");
    break;
  case 5:
    console.log("Friday");
    break;
  case 6:
    console.log("Saturday");
    break;
  default:
    console.log("Invalid day");
}

In this example, the switch statement checks the value of the day variable and executes the corresponding console.log() statement for the matching case. The break keyword is used to exit the switch statement once a match is found. The default case is executed if none of the other cases match.

For Loop

The for loop is used to repeatedly execute a block of code for a specified number of iterations:

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

In this example, the for loop iterates 5 times (from i = 0 to i < 5). The i++ statement increments the value of i by 1 after each iteration.

While Loop

The while loop is used to repeatedly execute a block of code as long as a specified condition is true:

let i = 0;

while (i < 5) {
  console.log(i);
  i++;
}

In this example, the while loop continues to execute the block of code until the value of i is no longer less than 5.