My Blog

Some examples of JS

Printing a message to the console:

class=”bg-black mb-4 rounded-md”>

console.log('Hello, world!');

 

Declaring a variable and assigning it a value:

var x = 10;

 

Declaring a function:

function greet(name) {
console.log('Hello, ' + name + '!');
}

 

Using an if statement to control the flow of execution:

if (x > 5) {
console.log('x is greater than 5');
} else {
console.log('x is less than or equal to 5');
}

 

Using a for loop to iterate over an array:

var arr = [1, 2, 3, 4, 5];
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}

 

Using an object to store data:

var person = {
name: 'John',
age: 30,
city: 'New York'
};
console.log(person.name);

 

Using the DOM API to manipulate HTML elements:

document.getElementById('my-element').innerHTML = 'Hello, world!';

 

Making an HTTP request with the fetch API:

fetch('https://example.com/api/endpoint')
.then(response => response.json())
.then(data => console.log(data));

 

Using a class to define a blueprint for creating objects:

class Vehicle {
constructor(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}

startEngine() {
console.log('Vroom!');
}
}

var car = new Vehicle('Toyota', 'Camry', 2020);
car.startEngine();