Introduction to JavaScript
JavaScript is a high-level, interpreted programming language that is one of the core technologies of the World Wide Web. It enables interactive web pages and can be used for server-side scripting, mobile app development, and more.
// This is a JavaScript comment.
console.log("Hello, World!");
let x = 10;
var y = 5;
console.log(x + y);
Variables and Data Types
JavaScript provides several primitive data types, including:
- Number: Represents numeric values.
- String: Represents textual data.
- Boolean: Represents truth values (true or false).
- Null: Represents the intentional absence of a value.
- Undefined: Represents a variable that has been declared but has not been assigned a value.
let name = "John Doe";
let age = 30;
let isStudent = false;
let address = null;
Control Flow
JavaScript allows you to control the flow of execution using conditional statements and loops.
- If...else statements: Execute different blocks of code based on a condition.
- Switch statements: A more concise alternative to multiple if...else statements.
- For loops: Repeat a block of code a specific number of times.
- While loops: Repeat a block of code as long as a condition is true.
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
for (let i = 0; i < 5; i++) {
console.log(i);
}