JavaScript is a high-level, often just-in-time compiled language that conforms to the ECMAScript specification. It is widely used for web development, enabling interactive web pages and dynamic content.

Basic Syntax

Here's a simple example of JavaScript syntax:

// A simple JavaScript function
function sayHello() {
  console.log("Hello, World!");
}

// Calling the function
sayHello();

Variables

JavaScript uses the var, let, and const keywords to declare variables. var is function-scoped or globally-scoped, let is block-scoped, and const is block-scoped and cannot be reassigned.

let message = "Hello, World!";
console.log(message);

Control Structures

JavaScript supports various control structures like if, else, for, while, and switch.

if (x > 5) {
  console.log("x is greater than 5");
} else {
  console.log("x is not greater than 5");
}

Objects

Objects in JavaScript are collections of properties, each with a name and a value. They are used to represent complex data structures.

let person = {
  name: "John",
  age: 30
};

console.log(person.name); // Outputs: John

Arrays

Arrays in JavaScript are ordered collections of values. They can store any type of data, including objects.

let fruits = ["Apple", "Banana", "Cherry"];

console.log(fruits[0]); // Outputs: Apple

Functions

Functions in JavaScript are reusable blocks of code that perform a specific task. They can be defined using the function keyword or arrow functions.

// Function declaration
function multiply(a, b) {
  return a * b;
}

// Function expression
const add = function(a, b) {
  return a + b;
};

// Arrow function
const subtract = (a, b) => a - b;

Events

JavaScript is commonly used to handle events on web pages, such as clicks, mouse movements, and key presses.

document.getElementById("myButton").addEventListener("click", function() {
  console.log("Button clicked!");
});

For more information on JavaScript events, visit the MDN Web Docs.

Conclusion

This guide provides a basic overview of JavaScript syntax. For further learning, explore the MDN Web Docs or dive into the JavaScript tutorials.


JavaScript Logo