1. Data Types:
Primitive Data Types:
String: Represents a sequence of characters, enclosed in single (
'
) or double ("
) quotes.javascriptlet greeting = "Hello, World!";
Number: Represents numeric values.
javascriptlet age = 25;
Boolean: Represents either
true
orfalse
.javascriptlet isStudent = true;
Undefined: Represents an uninitialized variable. If a variable is declared but not assigned a value, it is
undefined
.javascriptlet notDefined;
Null: Represents the intentional absence of any object value.
javascriptlet noValue = null;
Non-Primitive Data Types:
Object: Represents a collection of key-value pairs.
javascriptlet person = { name: "John", age: 30, isStudent: false };Array: Represents an ordered list of values.
javascriptlet fruits = ["apple", "banana", "orange"];
Function: Represents a reusable block of code.
javascriptfunction greet(name) { console.log("Hello, " + name + "!"); }
2. Variables:
Variables are used to store values. In JavaScript, you can declare variables using var
, let
, or const
.
var
(function-scoped): The original way to declare variables.let
(block-scoped): Introduced in ES6, allows reassignment.const
(block-scoped): Declares a variable with a constant value.
javascriptvar myVar = "Hello";
let myNumber = 42;
const pi = 3.14;
3. Operators:
Arithmetic Operators: Perform mathematical operations.
javascriptlet a = 5; let b = 2; console.log(a + b); // Addition: 7Comparison Operators: Compare values and return a boolean result.
javascriptlet x = 10; let y = 5; console.log(x > y); // Greater than: trueLogical Operators: Perform logical operations.
javascriptlet isTrue = true; let isFalse = false; console.log(isTrue && isFalse); // Logical AND: false
4. Control Conditional Statements:
if Statement: Executes a block of code if a specified condition is true.
javascriptlet num = 10; if (num > 0) { console.log("Number is positive"); }if...else Statement: Executes one block of code if the condition is true, and another if it's false.
javascriptlet num = -5; if (num > 0) { console.log("Number is positive"); } else { console.log("Number is non-positive"); }nested if...else Statement: Uses multiple if...else statements for complex conditions.
javascriptlet num = 0; if (num > 0) { console.log("Number is positive"); } else if (num < 0) { console.log("Number is negative"); } else { console.log("Number is zero"); }switch...case Statement: A switch statement allows a program to evaluate an expression and attempt to match the expression's value to a case label.
javascriptlet day = "Monday"; switch (day) { case "Monday": console.log("It's the start of the week"); break; case "Friday": console.log("It's almost the weekend"); break; default: console.log("It's a regular day"); }
5. Loops:
for Loop: Executes a block of code a specified number of times.
javascriptfor (let i = 0; i < 5; i++) { console.log(i); }while Loop: Executes a block of code as long as a specified condition is true.
javascriptlet i = 0; while (i < 5) { console.log(i); i++; }
6. Functions and Arrow Functions:
Function Declaration: Defines a function using the
function
keyword.javascriptfunction greet(name) { console.log("Hello, " + name + "!"); } greet("John");Arrow Function: A concise way to define functions introduced in ES6.
javascriptconst greet = (name) => { console.log(`Hello, ${name}!`); }; greet("John");
7. In-built Methods and Properties:
length: Returns the length of a string or the number of elements in an array.
javascriptlet str = "Hello, World!"; console.log(str.length); // Output: 13typeof: Returns a string representing the type of a variable.
javascriptlet num = 42; console.log(typeof num); // Output: "number"join(): Joins all elements of an array into a string.
javascriptlet arr = ["apple", "banana", "orange"]; let joinedStr = arr.join(", "); console.log(joinedStr); // Output: "apple, banana, orange"