Description:
Variables store data. JavaScript supports multiple data types: Number, String, Boolean, Object, Array, null, undefined, and Symbol.
Examples:
// Variable declaration using let and const
let age = 25; // Number
const name = "Alice"; // String
let isOnline = true; // Boolean
let user = { name: "Tom", age: 30 }; // Object
let scores = [90, 85, 88]; // Array
let emptyValue = null; // Null
let notDefined; // Undefined
console.log(name, age, isOnline);
Description:
JavaScript uses Promises and async/await to handle asynchronous tasks like API calls or timers.
Examples:
// Promise example
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => resolve("Data loaded"), 2000);
});
}
fetchData().then(result => console.log(result)); // Logs after 2 sec
// async/await example
async function loadData() {
const data = await fetchData();
console.log(data);
}
loadData(); // Logs: Data loaded