JavaScript Language Reference
Table of Contents
Introduction to JavaScript
JavaScript is a dynamic, weakly-typed, prototype-based, multi-paradigm programming language. It is most commonly used as a scripting language for web pages, but it is also used in many non-browser environments such as Node.js, server-side applications, and mobile apps.
This section provides a comprehensive reference to the core language features of JavaScript, covering syntax, semantics, and standard library objects.
Core Concepts
Understanding the fundamental building blocks of JavaScript is crucial for effective development.
Variables
Variables are containers for storing data values. In JavaScript, you declare variables using var, let, or const.
var: Function-scoped or globally scoped. Can be redeclared and updated.let: Block-scoped. Can be updated but not redeclared within its scope.const: Block-scoped. Cannot be reassigned or redeclared.
let message = "Hello, World!";
const PI = 3.14159;
var count = 10;
Data Types
JavaScript has several primitive data types and the object type:
- Primitive Types:
string,number,boolean,null,undefined,symbol,bigint. - Object Type: Includes arrays, functions, and general objects.
Operators
Operators are special symbols that perform operations on values and variables.
Common Operators
- Arithmetic:
+,-,*,/,% - Assignment:
=,+=,-=, etc. - Comparison:
==,===,!=,!==,>,<, etc. - Logical:
&&,||,! - Bitwise:
&,|,^,~,<<,>> - Type:
typeof,instanceof
Control Flow
Control flow statements determine the order in which code statements are executed.
- Conditional:
if...else,switch - Looping:
for,while,do...while,for...in,for...of - Branching:
break,continue,return
Functions
Functions are blocks of reusable code designed to perform a particular task.
function greet(name) {
return "Hello, " + name + "!";
}
const greetArrow = (name) => `Hello, ${name}!`;
console.log(greet("Alice"));
console.log(greetArrow("Bob"));
Objects
Objects are collections of properties, where each property is a key-value pair. Keys are usually strings.
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
console.log(person.firstName);
console.log(person.fullName());
Arrays
Arrays are special variables that can hold more than one value in a single variable. They are a type of object.
const fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // "Apple"
fruits.push("Date");
console.log(fruits.length); // 4
Classes
Classes are blueprints for creating objects. They were introduced in ECMAScript 2015 (ES6) as syntactic sugar over JavaScript's existing prototype-based inheritance.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
const dog = new Dog("Buddy");
dog.speak(); // Buddy barks.
Built-in Objects
JavaScript provides a rich set of built-in objects that offer predefined properties and methods for common tasks.
Object: The base constructor for all objects.Array: For working with arrays.String: For working with strings.Number: For working with numbers.Boolean: For working with boolean values.Date: For working with dates and times.Math: For mathematical operations.JSON: For parsing and stringifying JSON data.RegExp: For regular expression matching.Error: For handling runtime errors.
Refer to the Built-in Objects section for detailed documentation on each object.
Advanced Topics
Explore more advanced features and patterns in JavaScript:
- Asynchronous Programming (Promises, async/await)
- Closures
- Prototypes and Inheritance
- Modules (ES Modules)
- Error Handling
- Web APIs (DOM, Fetch, etc.)
Dive into specific topics through the sidebar navigation for a deeper understanding.