[Go to site: main page, start]

0% found this document useful (0 votes)
15 views6 pages

JavaScript Cheat Sheet

This JavaScript cheat sheet covers key concepts including variable declarations (let, var, const), data types, conditions, loops, scope, functions, arrays, objects, destructuring, and array methods. It provides examples for each concept, demonstrating syntax and usage. The document serves as a quick reference for JavaScript programming fundamentals.

Uploaded by

soudab30
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

JavaScript Cheat Sheet

This JavaScript cheat sheet covers key concepts including variable declarations (let, var, const), data types, conditions, loops, scope, functions, arrays, objects, destructuring, and array methods. It provides examples for each concept, demonstrating syntax and usage. The document serves as a quick reference for JavaScript programming fundamentals.

Uploaded by

soudab30
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript Cheat Sheet

1️⃣ Variables
let x = 10; // Can be reassigned, cannot be redeclared in the same
scope
var y = 20; // Can be reassigned and redeclared
const z = 30; // Cannot be reassigned

2️⃣ Data Types


let number = 10; // Number
let text = "Hello"; // String
let isActive = true; // Boolean
let empty = null; // Null
let notDefined; // Undefined
let user = { name: "kaiser" }; // Object
let list = [1, 2, 3]; // Array

JavaScript is dynamically typed.


Use typeof to check type:

typeof 10; // "number"


typeof "Hello"; // "string"

3️⃣ Conditions
if / else

let age = 30;

if (age === 30) {


[Link]("C'est 30");
} else {
[Link]("Ce n'est pas 30");
}
Ternary

age === 30 ? [Link]("C'est 30") : [Link]("Ce n'est pas 30");

switch

let grade = "B";

switch (grade) {
case "A": [Link]("Très bien"); break;
case "B": [Link]("Bien"); break;
default: [Link]("Réessayer");
}

4️⃣ Loops
for

for (let i = 0; i < 5; i++) [Link](i);

while

let i = 0;
while (i < 5) {
[Link](i);
i++;
}

do...while

let j = 0;
do {
[Link](j);
j++;
} while (j < 5);

do...while executes at least once.


5️⃣ Scope
Global Scope: accessible everywhere

let a = 10;
function test() {
[Link](a); // accessible
}

Local Scope: only inside function

function test() {
let b = 20;
[Link](b);
}
// [Link](b); ❌ Error

Block Scope ( let / const )

if (true) {
let c = 30;
const d = 40;
}
// [Link](c); ❌ Error

var ignores block scope (function-scoped only).

6️⃣ Functions
Default parameters & template literals

function sayHello(name = "personne", age = 0) {


[Link](`Bonjour ${name}`);
[Link](`Âge : ${age}`);
}
sayHello("Fatima Zahra", 30);

Types
1. Function Declaration
function add(a, b) { return a + b; }
[Link](add(2, 3));

2. Function Expression

const multiply = function(a, b) { return a * b; };


[Link](multiply(2, 3));

3. Arrow Function

const subtract = (a, b) => a - b;


[Link](subtract(5, 2));

7️⃣ Arrays
let fruits = ["pomme", "banane", "orange"];
[Link](fruits[0]); // "pomme"

// Add elements
[Link]("kiwi"); // end
[Link]("mangue"); // start

// Remove elements
[Link](); // end
[Link](); // start

// Length
[Link]([Link]);

Loop through array

for (let fruit of fruits) [Link](fruit);


[Link](fruit => [Link](fruit));

8️⃣ Objects
let person = { name: "kaiser", age: 30 };

// Access
[Link]([Link]);
[Link](person["age"]);

// Add or modify
[Link] = "Japane";
[Link] = 24;

// Delete
delete [Link];

9️⃣ Destructuring
Array destructuring

let numbers = [1, 2, 3];


let [a, b] = numbers; // a=1, b=2

// Spread operator
let moreNumbers = [ ... numbers, 4, 5]; // [1,2,3,4,5]

Object destructuring

let user = { name: "Ali", age: 30 };

// Destructure
let { name, age } = user;

// Spread operator
let newUser = { ... user, city: "Casablanca" };

10️⃣ Array Methods

let nums = [1, 2, 3, 4, 5];

// filter: elements that match condition


let even = [Link](n => n % 2 === 0); // [2,4]

// map: transform elements


let squares = [Link](n => n * n); // [1,4,9,16,25]

// reduce: single value from array


let sum = [Link]((acc, n) => acc + n, 0); // 15

// find: first matching element


let firstEven = [Link](n => n % 2 === 0); // 2

// some & every


[Link](n => n > 4); // true
[Link](n => n > 0); // true

You might also like