Let’s Learn
JavaScript
Bhavin Thummar
(function repeat() {
eat();
sleep();
love_yourself();
repeat();
})();
#JavaScript #Programming
Basic syntax in
JavaScript
// JavaScript basic syntax
Let variable = "Hello, World!";
console.log(variable);
// Output : Hello, World!
Variables and Types in
JavaScript
// Variables and Types
Let age = 25; // number
let temperature = -10.5; // number
let name = "John"; // string
let isStudent = true; // boolean
let fruits = ["Apple", "Banana", "Orange"]; // object
let person = {
name: "John", age: 25, isStudent: true
}; // object
function greet(name) { return "Apple"; } // function
let job; // undefined
let result = null; // object
const id = Symbol('id'); // symbol
Conditional Statements in
JavaScript
// Conditional Statements
let temperature = 25;
if (temperature > 30) {
console.log("It's hot outside!");
} else if (temperature > 20) {
console.log("It's warm outside.");
} else {
console.log("It's cold outside!");
}
// Output : It's warm outside.
Loops in
JavaScript
// Loops
for ( let i > 0; i < 5; i++) {
console.log("Count: ", i);
}
// Output :
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Functions in
JavaScript
// Functions
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Alice"));
// Output : Hello, Alice!
Arrays in
JavaScript
// Arrays
let fruits = ["Apple", "Banana", "Orange"];
console.log(fruits);
// Output : ['Apple', 'Banana', 'Orange']
console.log(fruits.length);
// Output : 3
Objects in
JavaScript
// Objects
let person = {
name: "John",
age: 30,
isStudent: false
};
console.log(person.name);
// Output : John
console.log(pe
rson.age);
// Output : 30
console.log(pe
rson.isSt
udent);
// Output : false
DOM Manipulation in
JavaScript
// DOM Manipulation
let heading = document.createElement("h1");
heading.textContent = "Hello, World!";
document.body.appendChild(heading);
// Output : <h1>Hello, World!</h1>
DOM Manipulation: JavaScript's ability to
modify webpage content and structure
dynamically.
Event Handling in
JavaScript
// Event Handling
let button = document.querySelector("button");
button.addEventListener("click", function() {
console.log("Button clicked!");
});
// Output : Button clicked!
Event Handling: JavaScript's capability to
respond to user actions on a webpage, such as
clicks or keypresses, by triggering predefined
functions or actions.
Asynchronous Programming in
JavaScript
// Asynchronous Programming (Promise)
function getData() {
return new Promise((resolve, reject) => {
// Simulating asynchronous task
setTimeout(() => {
resolve("Data fetched successfully!");
}, 2000);
});
}
getData().then(data => {
console.log(data);
});