Introduction
JavaScript is a cross-platform, object-oriented scripting language. It is a small and lightweight language. Inside a host environment (for example, a web browser), JavaScript can be connected to the objects of its environment to provide programmatic control over them.
JavaScript contains a standard library of objects, such as Array, Date, and Math, and a core set of language elements such as operators, control structures, and statements. Core JavaScript can be extended for a variety of purposes by supplementing it with additional objects; for example:
- 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.
What you should already know
This guide assumes you have the following basic background:
- A general understanding of the Internet and the World Wide Web (WWW).
- Good working knowledge of HyperText Markup Language (HTML).
- Some programming experience. If you are new to programming, try one of the tutorials linked on the main page about JavaScript.
JavaScript and Java
JavaScript and Java are similar in some ways but fundamentally different in others. JavaScript follows most Java expression syntax, naming conventions and basic control-flow constructs which was the reason why it was renamed from LiveScript to JavaScript.
In contrast to Java's compile-time system of classes, JavaScript supports a runtime system based on a small number of data types. It has a prototype-based object model instead of the more common class-based object model, providing dynamic inheritance.
Hello World
To get started, open your browser's developer console and write your first "Hello world" JavaScript code:
function greetMe(yourName) {
alert("Hello " + yourName);
}
greetMe("World");
Variables
You use variables as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules. A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9).
Declaring Variables
You can declare a variable in three ways: var
, let
, and const
.
// Using var (function-scoped)
var x = 42;
// Using let (block-scoped)
let y = 13;
// Using const (block-scoped, constant value)
const z = 3.14;
Variable Scope
When you declare a variable outside of any function, it is called a global variable. When you declare a variable within a function, it is called a local variable.
The let
and const
keywords, introduced in ECMAScript 2015, allow for block statement scope.
if (true) {
var x = 5; // x is function-scoped
}
console.log(x); // 5
if (true) {
let y = 5; // y is block-scoped
}
// console.log(y); // ReferenceError: y is not defined
Global Variables
Global variables are properties of the global object. In web pages, the global object is window
, so you can set and access global variables using the window.variable
syntax.
Constants
You can create a read-only, named constant with the const
keyword. A constant cannot change value through assignment or be re-declared while the script is running.
const PI = 3.14;
// PI = 1; // This will cause an error
Data Types
The latest ECMAScript standard defines seven data types:
- Primitives: Boolean, null, undefined, Number, String, Symbol.
- and Object
if...else Statement
Use the if
statement to execute a statement if a logical condition is true. Use the optional else
clause to execute a statement if the condition is false.
let condition = true;
if (condition) {
// statement_1 runs if condition is true
} else {
// statement_2 runs if condition is false
}
while Statement
A while statement executes its statements as long as a specified condition evaluates to true.
var n = 0;
var x = 0;
while (n < 3) {
n++;
x += n;
}
// After the loop, n will be 3 and x will be 6
Function Declarations
A function definition consists of the function
keyword, followed by the name of the function, a list of parameters, and the JavaScript statements that define the function.
function square(number) {
return number * number;
}
Reference
- All the documentation in this page is taken from MDN Web Docs