Certainly! JavaScript is a widely used programming language that is primarily used to make web pages interactive. It can be executed in web browsers to manipulate the Document Object Model (DOM), handle events, and communicate with servers. Here’s a brief overview and a simple example:
// Basic JavaScript syntax
// Variables
let greeting = "Hello, ";
const name = "John";
// Functions
function greetPerson(person) {
return greeting + person;
}
// Conditional statements
if (name === "John") {
console.log(greetPerson(name));
} else {
console.log("You are not John.");
}
// Loops
for (let i = 0; i < 3; i++) {
console.log("Count: " + i);
}
// Arrays
const fruits = ['apple', 'orange', 'banana'];
// Iterating through an array
for (let fruit of fruits) {
console.log(fruit);
}
// Objects
const personObject = {
firstName: "Jane",
lastName: "Doe",
age: 25
};
// Accessing object properties
console.log(personObject.firstName + " " + personObject.lastName);
// Event handling (in a browser environment)
document.getElementById("myButton").addEventListener("click", function() {
alert("Button clicked!");
});