JavaScript is a high-level, interpreted programming language that conforms to the ECMAScript specification. It is a language that is characterized by its versatility and widespread use in web development.
JavaScript enables interactive web pages and is an essential part of web applications. The majority of websites use it for client-side page behavior, and all major web browsers have a dedicated JavaScript engine to execute it.
console.log("Hello, World!");
As a multi-paradigm language, JavaScript supports event-driven, functional, and imperative programming styles. It has application programming interfaces (APIs) for working with text, dates, regular expressions, standard data structures, and the Document Object Model (DOM).
- Client-side JavaScript extends the core language by supplying objects to control a browser and its Document Object Model (DOM).
- Server-side JavaScript extends the core language by supplying objects relevant to running JavaScript on a server.
Variables are containers for storing data values. In JavaScript, you can declare variables using var, let, or const.
let name = "John";
const age = 30;
var isStudent = true;
JavaScript has dynamic types. This means that the same variable can be used to hold different data types.
JavaScript data types include:
- Number: integers and floating-point numbers
- String: textual data
- Boolean: true or false
- Null: intentional absence of value
- Undefined: variable declared but not assigned
- Object: complex data structures
- Symbol: unique identifier
Functions are one of the fundamental building blocks in JavaScript. A function is a set of statements that performs a task or calculates a value.
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("World"));
Functions can be defined in several ways:
- Function declarations: function name() {}
- Function expressions: const name = function() {};
- Arrow functions: const name = () => {};
Functions can take parameters and return values. In JavaScript, functions are first-class objects, meaning they can be passed as arguments to other functions, returned from functions, and assigned to variables.
const add = (a, b) => a + b;
console.log(add(5, 3)); // Output: 8
Control flow statements allow you to control the execution of your code based on certain conditions or repeatedly execute blocks of code.
let score = 85;
if (score >= 90) {
console.log("Grade A");
} else if (score >= 80) {
console.log("Grade B");
} else {
console.log("Grade C");
}
Conditional statements include if, else if, else, and switch. These allow your program to make decisions.
The switch statement is used to perform different actions based on different conditions.
let day = "Monday";
switch(day) {
case "Monday":
console.log("Start of week");
break;
case "Friday":
console.log("End of week");
break;
default:
console.log("Mid week");
}
Arrays are used to store multiple values in a single variable. They can hold any type of data.
let fruits = ["apple", "banana", "orange"];
console.log(fruits[0]); // Output: "apple"
fruits.push("grape");
console.log(fruits.length); // Output: 4
Objects are collections of key-value pairs. They can contain properties and methods.
let person = {
firstName: "John",
lastName: "Doe",
age: 30,
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
console.log(person.fullName());
You can access object properties using dot notation or bracket notation.
Loops are used to execute a block of code multiple times. JavaScript supports various types of loops.
// For loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// While loop
let j = 0;
while (j < 5) {
console.log(j);
j++;
}
Other loop types include:
- do...while: executes at least once
- for...in: iterates over object properties
- for...of: iterates over iterable objects
// For...of loop
let colors = ["red", "green", "blue"];
for (let color of colors) {
console.log(color);
}
Error handling is crucial for creating robust applications. JavaScript provides try-catch-finally blocks to handle runtime errors gracefully.
try {
let result = riskyOperation();
console.log(result);
} catch (error) {
console.log("An error occurred:", error.message);
} finally {
console.log("This always runs");
}
You can also create custom errors using the throw statement.
function validateAge(age) {
if (age < 0 || age > 150) {
throw new Error("Invalid age");
}
return true;
}
Proper error handling helps prevent your application from crashing and provides better user experience by displaying meaningful error messages.