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:
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:
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)
let sum = 5 + 3; // 8 let product = 4 * 2; // 8
2. Comparison Operators (to compare values):
- == (equal), != (not equal), >, <, >=, <=
console.log(10 > 5); // true console.log(10 == 5); // false
3. Logical Operators (for conditions):
- && (AND), || (OR), ! (NOT)
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:
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:
for (let i = 0; i < 5; i++) { console.log("Number:", i); }
2. while Loop:
let count = 0; while (count < 3) { console.log("Count:", count); count++; }
3. do...while Loop:
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:
function sayHello() { console.log("Hello!"); }
2. Call (run) the function:
sayHello(); // Outputs: Hello!
Functions with Parameters
Functions can take inputs (parameters) and return outputs:
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
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:
fruits.push("orange"); console.log(fruits); // Outputs: ["apple", "banana", "cherry", "orange"]
Objects
Objects are collections of key-value pairs.
How to Create an Object
let car = { brand: "Toyota", model: "Corolla", year: 2020 }; console.log(car.brand); // Outputs: Toyota
Adding or Modifying Properties
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:
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:
/* This is a multi-line comment */