--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 📜 JavaScript Commands --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 📦 require() Imports a module or file const express = require('express') 🚦 app.get(path, callback) Defines a route handler for HTTP GET requests app.get('/', (req, res) => { res.send('Hello World!') }) 📤 res.send(data) Sends a response to the client res.send('Hello World!') 🖥️ res.json(obj) Sends a JSON response res.json({ countries }) 🧩 app.use(middleware) Registers middleware for error or request handling app.use((err, req, res, next) => { res.status(500).json({ message: `Server Error: ${err}`}) }) 🔌 app.listen(port, callback) Starts server listening on port app.listen(8080, () => { console.log('Server started') }) 🗝️ Object.keys(obj) Gets array of property names from an object Object.keys(data) 🔁 array.map(callback) Transforms elements of an array countryCodes.map(code => ({ code, name: data[code].location })) 🔎 array.find(callback) Finds first element matching condition countryData.data.find(d => d.date === req.params.date) 🌐 req.params Accesses route parameters from URL req.params.country ❓ req.query Accesses query string parameters req.query.page ⏰ Date.now() Returns current timestamp in milliseconds const now = Date.now() 📡 XMLHttpRequest() Creates new AJAX request object const xhr = new XMLHttpRequest() 📬 xhr.open(method, url, async) Initializes AJAX request xhr.open('GET', url, true) 📤 xhr.send(data) Sends AJAX request xhr.send() 🧩 JSON.parse(text) Parses JSON text into a JavaScript object const obj = JSON.parse(xhr.responseText) 📝 document.write(text) Writes text/HTML directly to the page document.write('
Hello
') 🖥️ console.log(data) Outputs message to browser console console.log('Server running') 🌍 navigator.onLine Returns true/false if browser is online const online = navigator.onLine 🏠 window.location.hostname Gets current page's hostname const host = window.location.hostname ⏲️ setTimeout(func, delay) Executes function after delay setTimeout(() => { console.log('Hello') }, 1000) ❌ clearTimeout(id) Cancels a timeout set by setTimeout clearTimeout(timeoutId) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 📜 JavaScript Commands, Methods & Functions --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Command/Method What it Does Example Usage ----------------------------- -------------------------------------------------------- --------------------------------------------------------- 🚦 function name(params) { ... } Declares a named function function greet(name) { return 'Hi, ' + name; } ⚡ const func = () => { ... } Defines an arrow (anonymous) function const square = x => x * x; 🔁 array.map(callback) Applies callback to each array element, returns new array [1,2,3].map(n => n*2); // [2,4,6] 🔍 array.filter(callback) Filters array elements by callback condition [1,2,3,4].filter(n => n%2==0); // [2,4] 🔎 array.find(callback) Returns first element matching callback condition [1,2,3].find(n => n>1); // 2 🔄 array.forEach(callback) Executes function for each array element [1,2,3].forEach(n => console.log(n)); ➕ array.push(element) Adds element to end of array arr.push(4); ➖ array.pop() Removes last element and returns it const last = arr.pop(); 🚪 array.shift() Removes first element and returns it const first = arr.shift(); 🚪 array.unshift(element) Adds element to front of array arr.unshift(0); 🔢 array.reduce(callback, init) Reduces array to single value by accumulating callback [1,2,3].reduce((a,b)=>a+b,0); // 6 🔮 async function name(args) {} Declares an async function returning a Promise async function fetchData() { let res=await fetch(url); } 📡 fetch(url, options) Makes web request, returns a Promise fetch('/data').then(r => r.json()); 📥 Promise.then(success, fail) Handles Promise success or failure promise.then(result => console.log(result)); ⛄ Promise.catch(fail) Handles Promise failure promise.catch(error => console.error(error)); 🕒 setTimeout(func, delay) Runs a function once after delay in ms setTimeout(() => console.log('Delayed'), 1000); 🛑 clearTimeout(timeoutId) Clears a timeout set by setTimeout clearTimeout(myTimeout); 📚 class Name { ... } Defines an ES6 class class Animal { constructor(name) { this.name = name; } } 🔥 constructor(...) { ... } Special class initialization method constructor(name) { this.name = name; } ★ static method() { ... } Defines a static method on a class static info() { return 'Static info'; } 📌 this References current object or execution context this.name 🛠️ Object.keys(obj) Returns array of own property names Object.keys({a:1, b:2}); // ['a','b'] 🛠️ Object.values(obj) Returns array of own property values Object.values({a:1, b:2}); // [1,2] 🛠️ Object.entries(obj) Returns array of [key, value] pairs Object.entries({a:1, b:2}); // [['a',1],['b',2]] 💡 JSON.parse(str) Parses JSON string to object JSON.parse('{"x":1}') 💡 JSON.stringify(obj) Converts object to JSON string JSON.stringify({x:1}) 📰 console.log(data) Writes data to browser console console.log('Debug info') 🌐 window.location.href Gets or sets current URL window.location.href = 'https://example.com'; 🧭 navigator.onLine Boolean indicating if browser is online alert(navigator.onLine); 🖥️ document.getElementById(id) Gets DOM element by ID const el = document.getElementById('root'); 🖥️ document.querySelector(sel) Gets first DOM element matching selector document.querySelector('.className'); ⚙️ Element.addEventListener(ev, fn) Adds event listener to DOM element button.addEventListener('click', () => alert('Clicked')); 🔔 XMLHttpRequest() Creates AJAX request object const xhr = new XMLHttpRequest(); 📤 xhr.open(method, url, async) Initializes AJAX request xhr.open('GET', '/api/data', true); 📤 xhr.send(body) Sends AJAX request xhr.send(); 📅 Date.now() Returns milliseconds since epoch const timestamp = Date.now(); ⏳ new Date() Creates new Date object const d = new Date(); 🧮 Math.random() Returns random number 0 to 1 Math.random(); 📐 Math.floor(n) Rounds number down Math.floor(3.7); 📊 Math.ceil(n) Rounds number up Math.ceil(3.1); ⏹️ clearInterval(intervalId) Stops interval started by setInterval clearInterval(intervalId); ⏰ setInterval(func, delay) Runs function repeatedly at interval delay setInterval(() => console.log('tick'), 1000); 🐞 throw new Error(msg) Throws a custom error throw new Error('Error'); 📜 try { ... } catch(e) { ... } Exception handling try { ... } catch(e) { console.error(e); } 🛠️ require('module') Imports Node.js module const express = require('express'); 🚀 app.listen(port, callback) Starts Express server listening app.listen(8080, () => console.log('Server started')); 🚦 app.get(path, handler) Adds route handler for HTTP GET in Express app.get('/api', (req, res) => res.send('Hello')); 📦 module.exports = { ... } Exports module content module.exports = myFuncs; 🕵️♂️ process.env.VAR Access environment variable in Node.js console.log(process.env.PORT); ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------