var name = "John";
let age = 30;
const PI = 3.14;
let name = "Alice"; // String
let age = 25; // Number
let isStudent = true; // Boolean
let person = null; // Null
let job; // Undefined
let user = { name: "Bob", age: 30 }; // Object
let sum = 5 + 3;
let difference = 10 - 4;
let product = 2 * 6;
let quotient = 8 / 2;
let remainder = 10 % 3;
let x = 10;
x += 5; // Equivalent to: x = x + 5;
let a = 5;
let b = 10;
console.log(a === b); // false
console.log(a < b); // true
let isAdult = true;
let hasLicense = false;
console.log(isAdult && hasLicense); // false
console.log(isAdult || hasLicense); // true
let x = 10;
if (x > 5) {
console.log("x is greater than 5");
}
let age = 20;
if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are a minor");
}
let time = 14;
if (time < 12) {
console.log("Good morning");
} else if (time < 18) {
console.log("Good afternoon");
} else {
console.log("Good evening");
}
for (let i = 0; i < 5; i++) {
console.log("Iteration " + i);
}
let i = 0;
while (i < 5) {
console.log("Iteration " + i);
i++;
}
let j = 0;
do {
console.log("Iteration " + j);
j++;
} while (j < 5);
const person = { name: "Alice", age: 30 };
for (let key in person) {
console.log(key + ": " + person[key]);
}
const colors = ["red", "green", "blue"];
for (let color of colors) {
console.log(color);
}
function greet(name) {
return "Hello, " + name + "!";
}
let greeting = greet("Alice");
console.log(greeting); // Output: Hello, Alice!
function myFunction() {
let x = 10;
console.log(x); // Output: 10
}
myFunction();
console.log(x); // Error: x is not defined
function outerFunction() {
let outerVariable = "I'm from outer function";
function innerFunction() {
console.log(outerVariable); // Accessing outerVariable from outer scope
}
innerFunction();
}
outerFunction(); // Output: I'm from outer function
if (true) {
let y = 20;
const z = 30;
console.log(y); // Output: 20
console.log(z); // Output: 30
}
console.log(y); // Error: y is not defined
console.log(z); // Error: z is not defined
console.log(a); // Output: undefined
var a = 5;
var a;
console.log(a); // Output: undefined
a = 5;
let colors = ["red", "green", "blue"];
console.log(colors[0]); // Output: red
colors[1] = "yellow";
console.log(colors); // Output: ["red", "yellow", "blue"]
let person = {
name: "Alice",
age: 30,
isStudent: false
};
console.log(person.name); // Output: Alice
person.age = 35;
console.log(person); // Output: { name: "Alice", age: 35, isStudent: false }
person.city = "New York";
console.log(person); // Output: { name: "Alice", age: 35, isStudent: false, city: "New York" }
// Traditional function
function add(a, b) {
return a + b;
}
// Arrow function
const add = (a, b) => a + b;
const name = "Alice";
console.log(`Hello, ${name}!`);
const person = { name: "Bob", age: 30 };
const { name, age } = person;
console.log(name, age); // Output: Bob 30
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combinedArray = [...arr1, ...arr2];
console.log(combinedArray); // Output: [1, 2, 3, 4, 5, 6]
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3, 4, 5)); // Output: 15
const name = "Alice";
const age = 30;
const person = { name, age, greet() { console.log(`Hello, ${this.name}!`); } };
person.greet(); // Output: Hello, Alice!
const fetchData = () => {
return new Promise((resolve, reject) => {
// Simulating asynchronous operation
setTimeout(() => {
const dataIsAvailable = true;
if (dataIsAvailable) {
resolve("Data fetched successfully");
} else {
reject("Error fetching data");
}
}, 2000);
});
};
fetchData()
.then(data => console.log(data))
.catch(error => console.error(error));
< !DOCTYPE html >
< html lang="en" >
< head >
< meta charset="UTF-8" >
< meta name="viewport" content="width=device-width, initial-scale=1.0" >
< title > DOM Manipulation < /title >
< /head >
< body >
< div id="myDiv" class="container" >
< p > Hello, world! < /p >
< /div >
< script >
// Get element by ID
const divElement = document.getElementById("myDiv");
// Get element by CSS selector
const pElement = document.querySelector("p");
< /script >
< /body >
< /html >
//getElementById: Selects an element by its ID attribute.
const elementById = document.getElementById("myElementId");
//getElementsByClassName: Selects elements by their class name.
const elementsByClassName = document.getElementsByClassName("myClassName");
//getElementsByTagName: Selects elements by their tag name.
const elementsByTagName = document.getElementsByTagName("div");
//querySelector: Selects the first element that matches a specified CSS selector.
const elementByQuerySelector = document.querySelector("#myElementId");
//querySelectorAll: Selects all elements that match a specified CSS selector.
const elementsByQuerySelectorAll = document.querySelectorAll(".myClassName");
//textContent: Modifies the text content of an element.
element.textContent = "New text content";
//innerHTML: Modifies the HTML content of an element.
element.innerHTML = "< strong > New HTML content < /strong >"
//setAttribute: Sets the value of an attribute on an element.
element.setAttribute("href", "https://example.com");
//style: Modifies CSS properties of an element.
element.style.backgroundColor = "red";
// Add class
divElement.classList.add("highlight");
// Remove class
divElement.classList.remove("container");
// Add event listener
divElement.addEventListener("click", () => {
console.log("Div clicked");
});
// createElement: Creates a new element.
const newElement = document.createElement("div");
// Set text content
newParagraph.textContent = "This is a new paragraph";
// appendChild: Appends a child element to a parent element.
parentElement.appendChild(newElement);
// removeChild: Removes a child element from its parent.
parentElement.removeChild(childElement);
// Get all paragraphs in the document
const allParagraphs = document.querySelectorAll("p");
// Loop through and manipulate each paragraph
allParagraphs.forEach(paragraph => {
paragraph.style.color = "red";
});
const button = document.getElementById("myButton");
button.addEventListener("click", () => {
console.log("Button clicked");
});
button.addEventListener("click", (event) => {
console.log("Button clicked");
console.log("Target element:", event.target);
});
const input = document.getElementById("myInput");
input.addEventListener("keydown", (event) => {
console.log("Key pressed:", event.key);
});
const button = document.getElementById("myButton");
const handleClick = () => {
console.log("Button clicked");
};
button.addEventListener("click", handleClick);
// Later, remove the event listener
button.removeEventListener("click", handleClick);
function fetchData(callback) {
setTimeout(() => {
callback("Data fetched successfully");
}, 2000);
}
fetchData((data) => {
console.log(data);
});
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Data fetched successfully");
}, 2000);
});
}
fetchData()
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
});
async function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Data fetched successfully");
}, 2000);
});
}
async function fetchDataAndLog() {
try {
const data = await fetchData();
console.log(data);
} catch (error) {
console.error(error);
}
}
fetchDataAndLog();