JavaScript is the world's most popular programming language.
JavaScript is the programming language of the Web.
JavaScript is easy to learn.
This tutorial will teach you JavaScript from basic to advanced.
Start learning JavaScript now »
Examples in Each Chapter
With our "Try it Yourself" editor, you can edit the source code and view the result.
Use the Menu
We recommend reading this tutorial, in the sequence listed in the menu.
If you have a large screen, the menu will always be present on the left.
If you have a small screen, open the menu by clicking the top menu sign ☰.
Learn by Examples
Examples are better than 1000 words. Examples are often easier to understand than text explanations.
This tutorial supplements all explanations with clarifying "Try it Yourself" examples.
If you try all the examples, you will learn a lot about JavaScript, in a very short time!
JavaScript Examples »
Why Study JavaScript?
JavaScript is one of the 3 languages all web developers must learn:
1. HTML to define the content of web pages
2. CSS to specify the layout of web pages
3. JavaScript to program the behavior of web pages
This tutorial covers every version of JavaScript:
- The Original JavaScript ES1 ES2 ES3 [1997-1999]
- The First Main Revision ES5 [2009]
- The Second Revision ES6 [2015]
- The Yearly Additions [2016, 2017, 2018]
Learning Speed
In this tutorial, the learning speed is your choice.
Everything is up to you.
If you are struggling, take a break, or re-read the material.
Always make sure you understand all the "Try-it-Yourself" examples.
The only way to become a clever programmer is to: Practice. Practice. Practice. Code. Code. Code !
Commonly Asked Questions
- How do I get JavaScript?
- Where can I download JavaScript?
- Is JavaScript Free?
You don't have to get or download JavaScript.
JavaScript is already running in your browser on your computer, on your tablet, and on your smart-phone.
JavaScript is free to use for everyone.
My Learning
Track your progress with the free "My Learning" program here at W3Schools.
Log into your account, and start earning points!
This is an optional feature. You can study W3Schools without using My Learning.
JavaScript References
W3Schools maintains a complete JavaScript reference, including all HTML and browser objects.
The reference contains examples for all properties, methods and events, and is continuously updated according to the latest web standards.
Complete JavaScript Reference »
JavaScript Quiz Test
Test your JavaScript skills at W3Schools!
Start JavaScript Quiz!
Kickstart your career
Get certified by completing the course
Get certified
w3schoolsCERTIFIED.2022JavaScript functions are defined with the function
keyword.
You can use a function declaration or a function expression.
Function Declarations
Earlier in this tutorial, you learned that functions are declared with the following syntax:
function functionName[parameters] {
// code to be executed
}
Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are invoked [called upon].
Semicolons are used to separate executable JavaScript statements.
Since a function declaration is not an executable statement, it is not common to end it with a semicolon.
Function Expressions
A JavaScript function can also be defined using an expression.
A function expression can be stored in a variable:
After a function expression has been stored in a variable, the variable can be used as a function:
The function above is actually an anonymous function [a function without a name].
Functions stored in variables do not need function names. They are always invoked [called] using the variable name.
The function above ends with a semicolon because it is a part of an executable statement.
The Function[] Constructor
As you have seen in the
previous examples, JavaScript functions are defined with the function
keyword.
Functions can also be defined with a built-in JavaScript function constructor called Function[]
.
Example
const myFunction = new Function["a", "b", "return a * b"];
let x = myFunction[4, 3];
Try it Yourself »
You actually don't have to use the function constructor. The example above is the same as writing:
Example
const myFunction = function [a, b] {return a * b};
let x = myFunction[4, 3];
Try it Yourself »
Most of the time, you can avoid using the new
keyword in JavaScript.
Function Hoisting
Earlier in this tutorial, you learned about "hoisting" [JavaScript Hoisting].
Hoisting is JavaScript's default behavior of moving declarations to the top of the current scope.
Hoisting applies to variable declarations and to function declarations.
Because of this, JavaScript functions can be called before they are declared:
myFunction[5];
function myFunction[y] {
return y * y;
}
Functions defined using an expression are not hoisted.
Self-Invoking Functions
Function expressions can be made "self-invoking".
A self-invoking expression is invoked [started] automatically, without being called.
Function expressions will execute automatically if the expression is followed by [].
You cannot self-invoke a function declaration.
You have to add parentheses around the function to indicate that it is a function expression:
Example
[function [] {
let x = "Hello!!"; // I will invoke myself
}][];
Try it Yourself »
The function above is actually an anonymous self-invoking function [function without name].
Functions Can Be Used as Values
JavaScript functions can be used as values:
Example
function myFunction[a, b]
{
return a * b;
}
let x = myFunction[4, 3];
Try it Yourself »
JavaScript functions can be used in expressions:
Example
function myFunction[a, b] {
return a * b;
}
let x = myFunction[4, 3] * 2;
Try it Yourself »
Functions are Objects
The typeof
operator in JavaScript returns "function" for functions.
But, JavaScript functions can best be described as objects.
JavaScript functions have both properties and methods.
The arguments.length
property returns the number of arguments received when the function was invoked:
The toString[]
method returns the function as a string:
Example
function
myFunction[a, b] {
return a * b;
}
let text = myFunction.toString[];
Try it Yourself »
A function defined as the property of an object, is called a method to the object.
A function designed to create new objects, is called an object constructor.
Arrow Functions
Arrow functions allows a short syntax for writing function expressions.
You don't need the function
keyword, the return
keyword, and the curly brackets.
Example
// ES5
var x = function[x, y] {
return x * y;
}
// ES6
const x = [x, y] => x * y;
Try it Yourself »
Arrow functions do not have their own this
. They are not well suited for defining
object methods.
Arrow functions are not hoisted. They must be defined before they are used.
Using const
is safer than using var
, because a function expression is always constant value.
You can only omit the return
keyword and the curly brackets if the function is a single statement. Because of this, it might be a good habit to always keep them:
Arrow functions are not supported in IE11 or earlier.