Presenter: Aymen Ghemam Role: Lead Software Engineer at Smeetz Goal: It works on my machine
It works on my machine
// The Good const good = () => ({ flexibility: "Write frontend and backend", community: "Largest package ecosystem", evolution: "ES6+ modern features", async: "Non-blocking I/O", }); // The Bad typeof null; // 'object' (historical baggage) 0.1 + 0.2 === 0.3; // false (Eeeh, the good ol' floating point precision) // The Ugly console.log([] + []); // "" (wat?)
// Three ways to declare variables var oldSchool = "Open Minds"; // Function-scoped let modern = "Open Minds"; // Can be reassigned const constant = "Opem Minds"; // Cannot be reassigned
// Primitive Types let number = 42; // Number let text = "Hello"; // String let flag = true; // Boolean let nothing = null; // Null let undefined = undefined; // Undefined // Reference Types let array = [1, 2, 3]; // Array let object = { key: "value" }; // Object let func = () => {}; // Function
// Array operations let fruits = ["apple", "banana", "cherry", "orange"]; fruits.push("date"); // Add to end fruits.unshift("apricot"); // Add to start fruits.pop(); // Remove last fruits.shift(); // Remove first // Array methods fruits.map((fruit) => fruit.toUpperCase()); fruits.filter((fruit) => fruit.length > 5); fruits.find((fruit) => fruit === "apple");
// Object creation and manipulation let person = { name: "Alice", age: 30, greet() { return `Hello, I'm ${this.name}`; }, }; // Destructuring const { name, age } = person;
function sum(a, b) { return a + b; } const sum = (a, b) => a + b; const sum = function (a, b) { return a * b; };
// String methods let str = "Hello, World!"; str.toLowerCase(); // "hello, world!" str.includes("World"); // true str.replace("World", "Alice"); // "Hello, Alice!" str.split(","); // ["Hello", " World!"] // Array methods let numbers = [1, 2, 3, 4, 5]; numbers.reduce((a, b) => a + b, 0); // Sum numbers.find((n) => n > 3); // 4
// If-else and Switch if (condition) { // Do something } else if (anotherCondition) { // Do something else } else { // Default action } switch (value) { case 1: // Action for 1 break; default: // Default action }
// Different loop types for (let i = 0; i < 5; i++) { console.log(i); } const array = [1, 2, 3]; array.forEach((item) => console.log(item)); while (condition) { // Repeat while true }
// Global Scope let globalVar = "I'm global"; function exampleScope() { // Function Scope let functionVar = "Function specific"; if (true) { // Block Scope (let and const) let blockVar = "Block specific"; } }
class Animal { constructor(name) { this.name = name; } speak() { console.log(`${this.name} makes a sound`); } } class Dog extends Animal { bark() { console.log("Woof!"); } }
It represents the structure of a document as a tree of objects. Every web page is a document, and every element on the page is an object in the document.
// Selecting elements document.getElementById("myElement"); document.querySelector(".class"); document.querySelectorAll(".something"); document.getElementsByTagName("p"); // Changing content element.innerHTML = "New content"; element.style.color = "red"; element.classList.add("new-class"); element.classList.remove("old-class"); // Creating elements const newDiv = document.createElement("div"); parentElement.appendChild(newDiv);
// Event Handling button.addEventListener("click", (event) => { console.log("Button clicked!"); }); document.addEventListener("DOMContentLoaded", () => { // Page fully loaded });
More events: mouseover, mouseout, keydown, keyup, submit, change, etc.
mouseover
mouseout
keydown
keyup
submit
change
// Synchronous console.log("First"); console.log("Second"); console.log("Third"); // Asynchronous console.log("First"); setTimeout(() => console.log("Second"), 1000); console.log("Third");
// Promise function fetchData() { return new Promise((resolve, reject) => { // Async operation: takes some time if (success) { resolve("Done successfully"); } else { reject("Failed"); } }); } // Async/Await async function getData() { try { const result = await fetchData(); console.log(result); } catch (error) { console.error(error); } }
check /mini-game folder
/mini-game
const
let
Thank You!