[javascript]

JavaScript is a popular programming language for the web. It is used to add interactivity, functionality, and dynamic content to websites.

Syntax

JavaScript has a syntax similar to other programming languages such as C++ and Java. It uses variable declarations, control flow statements, loops, and functions.

Variables

In JavaScript, you can declare variables using the var, let, or const keywords. Variables can hold different types of values, such as numbers, strings, booleans, arrays, and objects.

var name = "John";
let age = 25;
const PI = 3.14;

console.log(name);
console.log(age);
console.log(PI);

Control Flow Statements

JavaScript includes if-else and switch statements for conditional execution of code. This allows you to control the flow of your program based on different conditions.

let hour = 13;

if (hour < 12) {
  console.log("Good morning!");
} else if (hour < 18) {
  console.log("Good afternoon!");
} else {
  console.log("Good evening!");
}

Loops

Loops are used to repeat a block of code multiple times. JavaScript has different types of loops, such as for, while, and do-while.

for (let i = 0; i < 5; i++) {
  console.log(i);
}

let i = 0;
while (i < 5) {
  console.log(i);
  i++;
}

let j = 0;
do {
  console.log(j);
  j++;
} while (j < 5);

Functions

Functions in JavaScript are reusable blocks of code that perform a specific task. They can take parameters and return values.

function greetings(name) {
  console.log("Hello, " + name + "!");
}

greetings("John");

function add(a, b) {
  return a + b;
}

let result = add(3, 5);
console.log(result);

Conclusion

JavaScript is a versatile and powerful programming language for building interactive websites. With its syntax, variables, control flow statements, loops, and functions, you can create dynamic and engaging web applications.

For more information, you can refer to the official JavaScript documentation provided by Mozilla.