In JavaScript, you can use the var, let, or const keywords to declare variables. Here are some basic data types:
// Variables
var myVariable = "Hello, World!";
let anotherVariable = 42;
const pi = 3.14;
// Data Types
let str = "String";
let num = 42;
let bool = true;
let arr = [1, 2, 3];
let obj = { key: "value" };
2. Operators
JavaScript supports various operators for arithmetic, comparison, and logical operations:
let a = 5;
let b = 10;
// Arithmetic operators
let sum = a + b;
let difference = a - b;
let product = a * b;
let quotient = a / b;
// Comparison operators
let isEqual = a === b;
let isNotEqual = a !== b;
let greaterThan = a > b;
let lessThan = a < b;
// Logical operators
let andOperator = true && false;
let orOperator = true || false;
let notOperator = !true;
3. Control Flow
Use if, else if, and else statements for control flow:
let x = 10;
if (x > 0) {
console.log("Positive");
} else if (x < 0) {
console.log("Negative");
} else {
console.log("Zero");
}
4. Loops
JavaScript supports for and while loops:
// For loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// While loop
let j = 0;
while (j < 5) {
console.log(j);
j++;
}
5. Functions
Define functions using the function keyword:
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("John");
6. Arrays and Objects
Arrays and objects are crucial data structures in JavaScript:
// Arrays
let fruits = ["apple", "banana", "orange"];
// Objects
let person = {
name: "John",
age: 25,
isStudent: false
};
This is just a brief overview. If you have specific questions or topics you'd like to dive deeper into, feel free to ask!