Basics

Variables

Variables are containers for storing data values, like numbers or words. You can create variables using var, let, or const.

How to Declare Variables

  • Use let for variables that can change.
  • Use const for variables that shouldn’t change.

Example:

Javascript
Copy
let age = 25;        // A variable that can change
const name = "Alice"; // A constant that cannot change
console.log(age, name);

Data Types

JavaScript supports different types of data. The most common are:

  • Numbers: 5, 3.14
  • Strings: "Hello", 'World'
  • Booleans: true, false
  • Objects: { key: "value" }
  • Arrays: [1, 2, 3]

Example:

Javascript
Copy
let number = 10;       // Number
let message = "Hi!";   // String
let isHappy = true;    // Boolean
let person = { name: "Alice", age: 25 }; // Object
let colors = ["red", "blue", "green"];   // Array

console.log(number, message, isHappy, person, colors);

Operators

Operators are symbols used to perform actions on variables and values.

Types of Operators

1. Arithmetic Operators (for math):

  • + (add), - (subtract), * (multiply), / (divide), % (remainder)
Javascript
Copy
let sum = 5 + 3;      // 8
let product = 4 * 2;  // 8

2. Comparison Operators (to compare values):

  • == (equal), != (not equal), >, <, >=, <=
Javascript
Copy
console.log(10 > 5); // true
console.log(10 == 5); // false

3. Logical Operators (for conditions):

  • && (AND), || (OR), ! (NOT)
Javascript
Copy
console.log(true && false); // false
console.log(true || false); // true

Conditionals

Conditionals allow you to run code based on certain conditions using if, else, and else if.

Example:

Javascript
Copy
let score = 80;

if (score > 90) {
  console.log("Excellent");
} else if (score > 50) {
  console.log("Good job");
} else {
  console.log("Try again");
}

Loops

Loops help you repeat a block of code multiple times.

Types of Loops

1. for Loop:

javascript
Copy
for (let i = 0; i < 5; i++) {
  console.log("Number:", i);
}

2. while Loop:

Javascript
Copy
let count = 0;
while (count < 3) {
  console.log("Count:", count);
  count++;
}

3. do...while Loop:

Javascript
Copy
let number = 0;
do {
  console.log("Number is:", number);
  number++;
} while (number < 3);

Functions

A function is a reusable block of code that performs a specific task.

How to Create a Function

1. Define the function:

Javascript
Copy
function sayHello() {
  console.log("Hello!");
}

2. Call (run) the function:

Javascript
Copy
sayHello(); // Outputs: Hello!

Functions with Parameters

Functions can take inputs (parameters) and return outputs:

Javascript
Copy
function addNumbers(a, b) {
  return a + b;
}

console.log(addNumbers(5, 3)); // Outputs: 8

Arrays

Arrays are lists of items.

How to Create an Array

Javascript
Copy
let fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // Outputs: apple

Common Array Methods

  • push() - Add an item to the end.
  • pop() - Remove the last item.
  • shift() - Remove the first item.
  • unshift() - Add an item to the start.

Example:

Javascript
Copy
fruits.push("orange");
console.log(fruits); // Outputs: ["apple", "banana", "cherry", "orange"]

Objects

Objects are collections of key-value pairs.

How to Create an Object

Javascript
Copy
let car = {
  brand: "Toyota",
  model: "Corolla",
  year: 2020
};
console.log(car.brand); // Outputs: Toyota

Adding or Modifying Properties

Javascript
Copy
car.color = "red"; // Add new property
car.year = 2021;   // Modify existing property
console.log(car);

Logging Output

Use console.log() to see what's happening in your code. It's great for debugging.

Example:

Javascript
Copy
let greeting = "Hello, JavaScript!";
console.log(greeting); // Outputs: Hello, JavaScript!

Comments

Comments help you describe your code. They are ignored by JavaScript.

  • Single-line comment: // This is a comment
  • Multi-line comment:
Javascript
Copy
/*
 This is a 
 multi-line comment
*/