JavaScript Tutorial
JavaScript is the programming language of HTML and the Web. JavaScript is easy to learn. This tutorial will teach you JavaScript from basic to advanced.Examples in Each Chapter
With our "Try it Yourself" editor, you can change all examples and view the results.Example
My First JavaScript
Try it Yourself » We recommend reading this tutorial, in the sequence listed in the left menu.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. JavaScript Examples If you try all the examples, you will learn a lot about JavaScript, in a very short time!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 is about JavaScript, and how JavaScript works with HTML and CSS.Did You Know?
JavaScript and Java are completely different languages, both in concept and design. JavaScript was invented by Brendan Eich in 1995, and became an ECMA standard in 1997.ECMA-262 is the official name of the standard. ECMAScript is the official name of the language. You can read more about the different JavaScript versions in the chapter JS Versions.Learning Speed
In this tutorial, the learning speed is your choice. Everything is up to you. If you are struggling, take a break, or reread the material. Always make sure you understand all the "Try-it-Yourself" examples.JavaScript References
W3Schools maintains a complete JavaScript reference, including all HTML and browser objects. The reference contains examples for all properties and methods, and is continuously updated according to the latest web standards. Complete JavaScript ReferenceJavaScript Quiz Test
Test your JavaScript skills at W3Schools! Start JavaScript Quiz! The HTML Certificate documents your knowledge of HTML. The CSS Certificate documents your knowledge of advanced CSS. The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM. The jQuery Certificate documents your knowledge of jQuery. The PHP Certificate documents your knowledge of PHP and SQL (MySQL). The XML Certificate documents your knowledge of XML, XML DOM and XSLT. The Bootstrap Certificate documents your knowledge of the Bootstrap framework.JavaScript Introduction
This page contains some examples of what JavaScript can do.JavaScript Can Change HTML Content
One of many JavaScript HTML methods is getElementById(). This example uses the method to "find" an HTML element (with id="demo") and changes the element content (innerHTML) to "Hello JavaScript":Example
document.getElementById("demo").innerHTML = "Hello JavaScript"; Try it Yourself » JavaScript accepts both double and single quotes:Example
document.getElementById('demo').innerHTML = 'Hello JavaScript'; Try it Yourself »JavaScript Can Change HTML Attributes
This example changes an HTML image by changing the src (source) attribute of an <img> tag:The Light Bulb
Try it Yourself »JavaScript Can Change HTML Styles (CSS)
Changing the style of an HTML element, is a variant of changing an HTML attribute:Example
document.getElementById("demo").style.fontSize = "35px"; ordocument.getElementById('demo').style.fontSize = '35px'; Try it Yourself »JavaScript Can Hide HTML Elements
Hiding HTML elements can be done by changing the display style:Example
document.getElementById("demo").style.display = "none"; ordocument.getElementById('demo').style.display = 'none'; Try it Yourself »JavaScript Can Show HTML Elements
Showing hidden HTML elements can also be done by changing the display style:Example
document.getElementById("demo").style.display = "block";ordocument.getElementById('demo').style.display = 'block'; Try it Yourself »JavaScript Where To
The <script> Tag
In HTML, JavaScript code must be inserted between <script> and </script> tags.Example
<script> document.getElementById("demo").innerHTML = "My First JavaScript"; </script> Try it Yourself » Old JavaScript examples may use a type attribute: <script type="text/javascript">. The type attribute is not required. JavaScript is the default scripting language in HTML.JavaScript Functions and Events
A JavaScript function is a block of JavaScript code, that can be executed when "called" for. For example, a function can be called when an event occurs, like when the user clicks a button. You will learn much more about functions and events in later chapters.JavaScript in <head> or <body>
You can place any number of scripts in an HTML document. Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both.JavaScript in <head>
In this example, a JavaScript function is placed in the <head> section of an HTML page. The function is invoked (called) when a button is clicked:Example
<!DOCTYPE html><html><head> <script> function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; } </script> </head> <body> <h1>A Web Page</h1><p id="demo">A Paragraph</p><button type="button" onclick="myFunction()">Try it</button> </body> </html> Try it Yourself »JavaScript in <body>
In this example, a JavaScript function is placed in the <body> section of an HTML page. The function is invoked (called) when a button is clicked:Example
<!DOCTYPE html> <html> <body> <h1>A Web Page</h1> <p id="demo">A Paragraph</p> <button type="button" onclick="myFunction()">Try it</button> <script> function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; } </script> </body> </html> Try it Yourself » Placing scripts at the bottom of the <body> element improves the display speed, because script compilation slows down the display.External JavaScript
Scripts can also be placed in external files:External file: myScript.js
function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; } External scripts are practical when the same code is used in many different web pages. JavaScript files have the file extension .js. To use an external script, put the name of the script file in the src (source) attribute of a <script> tag:Example
<script src="myScript.js"></script> Try it Yourself » You can place an external script reference in <head> or <body> as you like. The script will behave as if it was located exactly where the <script> tag is located. External scripts cannot contain <script> tags.External JavaScript Advantages
Placing scripts in external files has some advantages:
Keyword | Description |
---|---|
break | Terminates a switch or a loop |
continue | Jumps out of a loop and starts at the top |
debugger | Stops the execution of JavaScript, and calls (if available) the debugging function |
do ... while | Executes a block of statements, and repeats the block, while a condition is true |
for | Marks a block of statements to be executed, as long as a condition is true |
function | Declares a function |
if ... else | Marks a block of statements to be executed, depending on a condition |
return | Exits a function |
switch | Marks a block of statements to be executed, depending on different cases |
try ... catch | Implements error handling to a block of statements |
var | Declares a variable |
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (Remainder) |
++ | Increment |
-- | Decrement |
Operator | Example | Same As |
---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
Operator | Description |
---|---|
== | equal to |
=== | equal value and equal type |
!= | not equal |
!== | not equal value or not equal type |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
? | ternary operator |
Operator | Description |
---|---|
&& | logical and |
|| | logical or |
! | logical not |
Operator | Description |
---|---|
typeof | Returns the type of a variable |
instanceof | Returns true if an object is an instance of an object type |
Operator | Description | Example | Same as | Result | Decimal |
---|---|---|---|---|---|
& | AND | 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | OR | 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | NOT | ~ 5 | ~0101 | 1010 | 10 |
^ | XOR | 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
<< | Zero fill left shift | 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | Signed right shift | 5 >> 1 | 0101 >> 1 | 0010 | 2 |
>>> | Zero fill right shift | 5 >>> 1 | 0101 >>> 1 | 0010 | 2 |
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (Remainder) |
++ | Increment |
-- | Decrement |
Operand | Operator | Operand |
---|---|---|
100 | + | 50 |
Value | Operator | Description | Example |
---|---|---|---|
20 | ( ) | Expression grouping | (3 + 4) |
19 | . | Member | person.name |
19 | [] | Member | person["name"] |
19 | () | Function call | myFunction() |
19 | new | Create | new Date() |
17 | ++ | Postfix Increment | i++ |
17 | -- | Postfix Decrement | i-- |
16 | ++ | Prefix Increment | ++i |
16 | -- | Prefix Decrement | --i |
16 | ! | Logical not | !(x==y) |
15 | typeof | Type | typeof x |
15 | ** | Exponentiation | 10 ** 2 |
14 | * | Multiplication | 10 * 5 |
14 | / | Division | 10 / 5 |
14 | % | Modulo division | 10 % 5 |
13 | + | Addition | 10 + 5 |
13 | - | Subtraction | 10 - 5 |
12 | << | Shift left | x << 2 |
12 | >> | Shift right | x >> 2 |
12 | >>> | Shift right (unsigned) | x >>> 2 |
11 | < | Less than | x < y |
11 | <= | Less than or equal | x <= y |
11 | > | Greater than | x > y |
11 | >= | Greater than or equal | x >= y |
10 | == | Equal | x == y |
10 | === | Strict equal | x === y |
10 | != | Unequal | x != y |
10 | !== | Strict unequal | x !== y |
6 | && | Logical and | x && y |
5 | || | Logical or | x || y |
3 | = | Assignment | x = y |
3 | += | Assignment | x += y |
3 | -= | Assignment | x -= y |
3 | *= | Assignment | x *= y |
3 | %= | Assignment | x %= y |
3 | <<= | Assignment | x <<= y |
3 | >>= | Assignment | x >>= y |
3 | >>>= | Assignment | x >>>= y |
3 | &= | Assignment | x &= y |
3 | ^= | Assignment | x ^= y |
3 | |= | Assignment | x |= y |
Operator | Example | Same As |
---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
<<= | x <<= y | x = x << y |
>>= | x >>= y | x = x >> y |
>>>= | x >>>= y | x = x >>> y |
&= | x &= y | x = x & y |
^= | x ^= y | x = x ^ y |
|= | x |= y | x = x | y |
**= | x **= y | x = x ** y |
Object | Properties | Methods |
---|---|---|
car.name = Fiat car.model = 500 car.weight = 850kg car.color = white | car.start() car.drive() car.brake() car.stop() |
Property | Property Value |
---|---|
firstName | John |
lastName | Doe |
age | 50 |
eyeColor | blue |
Property | Property Value |
---|---|
firstName | John |
lastName | Doe |
age | 50 |
eyeColor | blue |
fullName | function() {return this.firstName + " " + this.lastName;} |
Event | Description |
---|---|
onchange | An HTML element has been changed |
onclick | The user clicks an HTML element |
onmouseover | The user moves the mouse over an HTML element |
onmouseout | The user moves the mouse away from an HTML element |
onkeydown | The user pushes a keyboard key |
onload | The browser has finished loading the page |
Code | Result | Description |
---|---|---|
\' | ' | Single quote |
\" | " | Double quote |
\\ | \ | Backslash |
Code | Result |
---|---|
\b | Backspace |
\f | Form Feed |
\n | New Line |
\r | Carriage Return |
\t | Horizontal Tabulator |
\v | Vertical Tabulator |
Value (aka Fraction/Mantissa) | Exponent | Sign |
---|---|---|
52 bits (0 - 51) | 11 bits (52 - 62) | 1 bit (63) |
Method | Description |
---|---|
Number() | Returns a number, converted from its argument. |
parseFloat() | Parses its argument and returns a floating point number |
parseInt() | Parses its argument and returns an integer |
Property | Description |
---|---|
MAX_VALUE | Returns the largest number possible in JavaScript |
MIN_VALUE | Returns the smallest number possible in JavaScript |
NEGATIVE_INFINITY | Represents negative infinity (returned on overflow) |
NaN | Represents a "Not-a-Number" value |
POSITIVE_INFINITY | Represents infinity (returned on overflow) |
Type | Example |
---|---|
ISO Date | "2015-03-25" (The International Standard) |
Short Date | "03/25/2015" |
Long Date | "Mar 25 2015" or "25 Mar 2015" |
Method | Description |
---|---|
getFullYear() | Get the year as a four digit number (yyyy) |
getMonth() | Get the month as a number (0-11) |
getDate() | Get the day as a number (1-31) |
getHours() | Get the hour (0-23) |
getMinutes() | Get the minute (0-59) |
getSeconds() | Get the second (0-59) |
getMilliseconds() | Get the millisecond (0-999) |
getTime() | Get the time (milliseconds since January 1, 1970) |
getDay() | Get the weekday as a number (0-6) |
Method | Description |
---|---|
getUTCDate() | Same as getDate(), but returns the UTC date |
getUTCDay() | Same as getDay(), but returns the UTC day |
getUTCFullYear() | Same as getFullYear(), but returns the UTC year |
getUTCHours() | Same as getHours(), but returns the UTC hour |
getUTCMilliseconds() | Same as getMilliseconds(), but returns the UTC milliseconds |
getUTCMinutes() | Same as getMinutes(), but returns the UTC minutes |
getUTCMonth() | Same as getMonth(), but returns the UTC month |
getUTCSeconds() | Same as getSeconds(), but returns the UTC seconds |
Method | Description |
---|---|
setDate() | Set the day as a number (1-31) |
setFullYear() | Set the year (optionally month and day) |
setHours() | Set the hour (0-23) |
setMilliseconds() | Set the milliseconds (0-999) |
setMinutes() | Set the minutes (0-59) |
setMonth() | Set the month (0-11) |
setSeconds() | Set the seconds (0-59) |
setTime() | Set the time (milliseconds since January 1, 1970) |
Method | Description |
---|---|
abs(x) | Returns the absolute value of x |
acos(x) | Returns the arccosine of x, in radians |
asin(x) | Returns the arcsine of x, in radians |
atan(x) | Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians |
atan2(y, x) | Returns the arctangent of the quotient of its arguments |
ceil(x) | Returns the value of x rounded up to its nearest integer |
cos(x) | Returns the cosine of x (x is in radians) |
exp(x) | Returns the value of Ex |
floor(x) | Returns the value of x rounded down to its nearest integer |
log(x) | Returns the natural logarithm (base E) of x |
max(x, y, z, ..., n) | Returns the number with the highest value |
min(x, y, z, ..., n) | Returns the number with the lowest value |
pow(x, y) | Returns the value of x to the power of y |
random() | Returns a random number between 0 and 1 |
round(x) | Returns the value of x rounded to its nearest integer |
sin(x) | Returns the sine of x (x is in radians) |
sqrt(x) | Returns the square root of x |
tan(x) | Returns the tangent of an angle |
Operator | Description | Example |
---|---|---|
== | equal to | if (day == "Monday") |
> | greater than | if (salary > 9000) |
< | less than | if (age < 18) |
Operator | Description | Comparing | Returns | Try it |
---|---|---|---|---|
== | equal to | x == 8 | false | Try it » |
x == 5 | true | Try it » | ||
x == "5" | true | Try it » | ||
=== | equal value and equal type | x === 5 | true | Try it » |
x === "5" | false | Try it » | ||
!= | not equal | x != 8 | true | Try it » |
!== | not equal value or not equal type | x !== 5 | false | Try it » |
x !== "5" | true | Try it » | ||
x !== 8 | true | Try it » | ||
> | greater than | x > 8 | false | Try it » |
< | less than | x < 8 | true | Try it » |
>= | greater than or equal to | x >= 8 | false | Try it » |
<= | less than or equal to | x <= 8 | true | Try it » |
Operator | Description | Example | Try it |
---|---|---|---|
&& | and | (x < 10 && y > 1) is true | Try it » |
|| | or | (x == 5 || y == 5) is false | Try it » |
! | not | !(x == y) is true | Try it » |
Case | Value | Try |
---|---|---|
2 < 12 | true | Try it » |
2 < "12" | true | Try it » |
2 < "John" | false | Try it » |
2 > "John" | false | Try it » |
2 == "John" | false | Try it » |
"2" < "12" | false | Try it » |
"2" > "12" | true | Try it » |
"2" == "12" | false | Try it » |
Method | Description |
---|---|
toExponential() | Returns a string, with a number rounded and written using exponential notation. |
toFixed() | Returns a string, with a number rounded and written with a specified number of decimals. |
toPrecision() | Returns a string, with a number written with a specified length |
Method | Description |
---|---|
getDate() | Get the day as a number (1-31) |
getDay() | Get the weekday a number (0-6) |
getFullYear() | Get the four digit year (yyyy) |
getHours() | Get the hour (0-23) |
getMilliseconds() | Get the milliseconds (0-999) |
getMinutes() | Get the minutes (0-59) |
getMonth() | Get the month (0-11) |
getSeconds() | Get the seconds (0-59) |
getTime() | Get the time (milliseconds since January 1, 1970) |
Method | Description |
---|---|
parseFloat() | Parses a string and returns a floating point number |
parseInt() | Parses a string and returns an integer |
OriginalValue | Convertedto Number | Convertedto String | Convertedto Boolean | Try it |
---|---|---|---|---|
false | 0 | "false" | false | Try it » |
true | 1 | "true" | true | Try it » |
0 | 0 | "0" | false | Try it » |
1 | 1 | "1" | true | Try it » |
"0" | 0 | "0" | true | Try it » |
"000" | 0 | "000" | true | Try it » |
"1" | 1 | "1" | true | Try it » |
NaN | NaN | "NaN" | false | Try it » |
Infinity | Infinity | "Infinity" | true | Try it » |
-Infinity | -Infinity | "-Infinity" | true | Try it » |
"" | 0 | "" | false | Try it » |
"20" | 20 | "20" | true | Try it » |
"twenty" | NaN | "twenty" | true | Try it » |
[ ] | 0 | "" | true | Try it » |
[20] | 20 | "20" | true | Try it » |
[10,20] | NaN | "10,20" | true | Try it » |
["twenty"] | NaN | "twenty" | true | Try it » |
["ten","twenty"] | NaN | "ten,twenty" | true | Try it » |
function(){} | NaN | "function(){}" | true | Try it » |
{ } | NaN | "[object Object]" | true | Try it » |
null | 0 | "null" | false | Try it » |
undefined | NaN | "undefined" | false | Try it » |
Operator | Name | Description |
---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shifts left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
>>> | Zero fill right shift | Shifts right by pushing zeros in from the left, and let the rightmost bits fall off |
Operation | Result | Same as | Result |
---|---|---|---|
5 & 1 | 1 | 0101 & 0001 | 0001 |
5 | 1 | 5 | 0101 | 0001 | 0101 |
~ 5 | 10 | ~0101 | 1010 |
5 << 1 | 10 | 0101 << 1 | 1010 |
5 ^ 1 | 4 | 0101 ^ 0001 | 0100 |
5 >> 1 | 2 | 0101 >> 1 | 0010 |
5 >>> 1 | 2 | 0101 >>> 1 | 0010 |
Operation | Result |
---|---|
0 & 0 | 0 |
0 & 1 | 0 |
1 & 0 | 0 |
1 & 1 | 1 |
Operation | Result |
---|---|
1111 & 0000 | 0000 |
1111 & 0001 | 0001 |
1111 & 0010 | 0010 |
1111 & 0100 | 0100 |
Operation | Result |
---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Operation | Result |
---|---|
1111 | 0000 | 1111 |
1111 | 0001 | 1111 |
1111 | 0010 | 1111 |
1111 | 0100 | 1111 |
Operation | Result |
---|---|
0 ^ 0 | 0 |
0 ^ 1 | 1 |
1 ^ 0 | 1 |
1 ^ 1 | 0 |
Operation | Result |
---|---|
1111 ^ 0000 | 1111 |
1111 ^ 0001 | 1110 |
1111 ^ 0010 | 1101 |
1111 ^ 0100 | 1011 |
Decimal | Binary |
---|---|
5 | 00000000000000000000000000000101 |
1 | 00000000000000000000000000000001 |
5 & 1 | 00000000000000000000000000000001 (1) |
Decimal | Binary |
---|---|
5 | 00000000000000000000000000000101 |
1 | 00000000000000000000000000000001 |
5 | 1 | 00000000000000000000000000000101 (5) |
Decimal | Binary |
---|---|
5 | 00000000000000000000000000000101 |
1 | 00000000000000000000000000000001 |
5 ^ 1 | 00000000000000000000000000000100 (4) |
Decimal | Binary |
---|---|
5 | 00000000000000000000000000000101 |
~5 | 11111111111111111111111111111010 (-6) |
Decimal | Binary |
---|---|
5 | 00000000000000000000000000000101 |
5 << 1 | 00000000000000000000000000001010 (10) |
Decimal | Binary |
---|---|
-5 | 11111111111111111111111111111011 |
-5 >> 1 | 11111111111111111111111111111101 (-3) |
Decimal | Binary |
---|---|
5 | 00000000000000000000000000000101 |
5 >>> 1 | 00000000000000000000000000000010 (2) |
Binary Representation | Decimal value |
---|---|
00000000000000000000000000000001 | 1 |
00000000000000000000000000000010 | 2 |
00000000000000000000000000000100 | 4 |
00000000000000000000000000001000 | 8 |
00000000000000000000000000010000 | 16 |
00000000000000000000000000100000 | 32 |
00000000000000000000000001000000 | 64 |
Binary Representation | Decimal value |
---|---|
00000000000000000000000000000101 | 5 (4 + 1) |
00000000000000000000000000001101 | 13 (8 + 4 + 1) |
00000000000000000000000000101101 | 45 (32 + 8 + 4 + 1) |
Binary Representation | Decimal value |
---|---|
00000000000000000000000000000101 | 5 |
11111111111111111111111111111011 | -5 |
00000000000000000000000000000110 | 6 |
11111111111111111111111111111010 | -6 |
00000000000000000000000000101000 | 40 |
11111111111111111111111111011000 | -40 |
Modifier | Description |
---|---|
i | Perform case-insensitive matching |
g | Perform a global match (find all matches rather than stopping after the first match) |
m | Perform multiline matching |
Expression | Description |
---|---|
[abc] | Find any of the characters between the brackets |
[0-9] | Find any of the digits between the brackets |
(x|y) | Find any of the alternatives separated with | |
Metacharacter | Description |
---|---|
\d | Find a digit |
\s | Find a whitespace character |
\b | Find a match at the beginning or at the end of a word |
\uxxxx | Find the Unicode character specified by the hexadecimal number xxxx |
Quantifier | Description |
---|---|
n+ | Matches any string that contains at least one n |
n* | Matches any string that contains zero or more occurrences of n |
n? | Matches any string that contains zero or one occurrences of n |
Property | Description |
---|---|
name | Sets or returns an error name |
message | Sets or returns an error message (a string) |
Error Name | Description |
---|---|
EvalError | An error has occurred in the eval() function |
RangeError | A number "out of range" has occurred |
ReferenceError | An illegal reference has occurred |
SyntaxError | A syntax error has occurred |
TypeError | A type error has occurred |
URIError | An error in encodeURI() has occurred |
abstract | arguments | await* | boolean |
break | byte | case | catch |
char | class* | const | continue |
debugger | default | delete | do |
double | else | enum* | eval |
export* | extends* | false | final |
finally | float | for | function |
goto | if | implements | import* |
in | instanceof | int | interface |
let* | long | native | new |
null | package | private | protected |
public | return | short | static |
super* | switch | synchronized | this |
throw | throws | transient | true |
try | typeof | var | void |
volatile | while | with | yield |
abstract | boolean | byte | char |
double | final | float | goto |
int | long | native | short |
synchronized | throws | transient | volatile |
Array | Date | eval | function |
hasOwnProperty | Infinity | isFinite | isNaN |
isPrototypeOf | length | Math | NaN |
name | Number | Object | prototype |
String | toString | undefined | valueOf |
getClass | java | JavaArray | javaClass |
JavaObject | JavaPackage |
alert | all | anchor | anchors |
area | assign | blur | button |
checkbox | clearInterval | clearTimeout | clientInformation |
close | closed | confirm | constructor |
crypto | decodeURI | decodeURIComponent | defaultStatus |
document | element | elements | embed |
embeds | encodeURI | encodeURIComponent | escape |
event | fileUpload | focus | form |
forms | frame | innerHeight | innerWidth |
layer | layers | link | location |
mimeTypes | navigate | navigator | frames |
frameRate | hidden | history | image |
images | offscreenBuffering | open | opener |
option | outerHeight | outerWidth | packages |
pageXOffset | pageYOffset | parent | parseFloat |
parseInt | password | pkcs11 | plugin |
prompt | propertyIsEnum | radio | reset |
screenX | screenY | scroll | secure |
select | self | setInterval | setTimeout |
status | submit | taint | text |
textarea | top | unescape | untaint |
window |
onblur | onclick | onerror | onfocus |
onkeydown | onkeypress | onkeyup | onmouseover |
onload | onmouseup | onmousedown | onsubmit |
Year | Name | Description |
---|---|---|
1997 | ECMAScript 1 | First Edition. |
1998 | ECMAScript 2 | Editorial changes only. |
1999 | ECMAScript 3 | Added Regular Expressions.Added try/catch. |
ECMAScript 4 | Was never released. | |
2009 | ECMAScript 5 | Added "strict mode".Added JSON support. |
2011 | ECMAScript 5.1 | Editorial changes. |
2015 | ECMAScript 6 | Added classes and modules. |
2016 | ECMAScript 7 | Added exponential operator (**). Added Array.prototype.includes. |
Engine | ECMA | Browser |
---|---|---|
V8 | 6 | Chrome (Partial Support) |
SpiderMonkey | 6 | Firefox (Partial Support) |
Chakra | 6 | Edge (Partial Support) |
Nitro | 6 | Safari (Partial Support) |
V8 | 6 | Opera (Partial Support) |
V8 | 5 | Chrome 23 |
SpiderMonkey | 5 | Firefox 21 |
JavaScript 1.8.5 | 5 | Firefox 4 |
Nitro | 5 | Safari 6 |
V8 | 5 | Opera 15 |
Chakra | 5 | Edge 12 |
Chakra | 5 | IE 10 |
Year | JavaScript | ECMA | JScript | Browser |
---|---|---|---|---|
1996 | 1.0 | Netscape 2 | ||
1996 | 1.0 | Internet Explorer 3 | ||
1996 | 1.1 | Netscape 3 | ||
1997 | 1.2 | Netscape 4 | ||
1997 | 1 | 3.0 | Internet Explorer 4 | |
1998 | 1.3 | 1 | Netscape 4.06 | |
1999 | 2 | 5.0 | Internet Explorer 5 | |
1999 | 2 | 5.1 | Internet Explorer 5.1 | |
2000 | 3 | 5.5 | Internet Explorer 5.5 | |
2000 | 1.5 | 3 | Netscape 6 | |
2000 | 1.5 | 3 | Firefox 1 | |
2001 | 3 | 5.6 | Internet Explorer 6 | |
2006 | 3 | 5.7 | Internet Explorer 7 | |
2005 | 1.6 | 3 | Firefox 1.5 | |
2006 | 1.7 | 3 | Firefox 2 | |
2008 | 1.8 | 3 | Firefox 3 | |
2009 | 3 | 5.8 | Internet Explorer 8 | |
2009 | 1.8.1 | 5 | Firefox 3.5 (Partial Support) | |
2009 | 1.8.2 | 5 | Firefox 3.6 (Partial Support) | |
2011 | 1.8.5 | 5 | Firefox 4 | |
2011 | 5 | 9.0 | Internet Explorer 9 (Partial Support) |
Attribute | Description |
---|---|
disabled | Specifies that the input element should be disabled |
max | Specifies the maximum value of an input element |
min | Specifies the minimum value of an input element |
pattern | Specifies the value pattern of an input element |
required | Specifies that the input field requires an element |
type | Specifies the type of an input element |
Selector | Description |
---|---|
:disabled | Selects input elements with the "disabled" attribute specified |
:invalid | Selects input elements with invalid values |
:optional | Selects input elements with no "required" attribute specified |
:required | Selects input elements with the "required" attribute specified |
:valid | Selects input elements with valid values |
Property | Description |
---|---|
checkValidity() | Returns true if an input element contains valid data. |
setCustomValidity() | Sets the validationMessage property of an input element. |
Property | Description |
---|---|
validity | Contains boolean properties related to the validity of an input element. |
validationMessage | Contains the message a browser will display when the validity is false. |
willValidate | Indicates if an input element will be validated. |
Property | Description |
---|---|
customError | Set to true, if a custom validity message is set. |
patternMismatch | Set to true, if an element's value does not match its pattern attribute. |
rangeOverflow | Set to true, if an element's value is greater than its max attribute. |
rangeUnderflow | Set to true, if an element's value is less than its min attribute. |
stepMismatch | Set to true, if an element's value is invalid per its step attribute. |
tooLong | Set to true, if an element's value exceeds its maxLength attribute. |
typeMismatch | Set to true, if an element's value is invalid per its type attribute. |
valueMissing | Set to true, if an element (with a required attribute) has no value. |
valid | Set to true, if an element's value is valid. |
Value | Type | Comment |
---|---|---|
"Hello" | string | "Hello" is always "Hello" |
3.14 | number | 3.14 is always 3.14 |
true | boolean | true is always true |
false | boolean | false is always false |
null | null (object) | null is always null |
undefined | undefined | undefined is always undefined |
Property | Value |
---|---|
firstName | John |
lastName | Doe |
age | 50 |
eyeColor | blue |
Property | Value |
---|---|
firstName | John |
lastName | Doe |
age | 50 |
eyeColor | blue |
fullName | function() {return this.firstName + " " + this.lastName;} |
Property | Value |
---|---|
firstName | John |
lastName | Doe |
age | 50 |
eyeColor | blue |
fullName | function() {return this.firstName + " " + this.lastName;} |
Method | Description |
---|---|
document.getElementById(id) | Find an element by element id |
document.getElementsByTagName(name) | Find elements by tag name |
document.getElementsByClassName(name) | Find elements by class name |
Method | Description |
---|---|
element.innerHTML = new html content | Change the inner HTML of an element |
element.attribute = new value | Change the attribute value of an HTML element |
element.setAttribute(attribute, value) | Change the attribute value of an HTML element |
element.style.property = new style | Change the style of an HTML element |
Method | Description |
---|---|
document.createElement(element) | Create an HTML element |
document.removeChild(element) | Remove an HTML element |
document.appendChild(element) | Add an HTML element |
document.replaceChild(element) | Replace an HTML element |
document.write(text) | Write into the HTML output stream |
Method | Description |
---|---|
document.getElementById(id).onclick = function(){code} | Adding event handler code to an onclick event |
Property | Description | DOM |
---|---|---|
document.anchors | Returns all <a> elements that have a name attribute | 1 |
document.applets | Returns all <applet> elements (Deprecated in HTML5) | 1 |
document.baseURI | Returns the absolute base URI of the document | 3 |
document.body | Returns the <body> element | 1 |
document.cookie | Returns the document's cookie | 1 |
document.doctype | Returns the document's doctype | 3 |
document.documentElement | Returns the <html> element | 3 |
document.documentMode | Returns the mode used by the browser | 3 |
document.documentURI | Returns the URI of the document | 3 |
document.domain | Returns the domain name of the document server | 1 |
document.domConfig | Obsolete. Returns the DOM configuration | 3 |
document.embeds | Returns all <embed> elements | 3 |
document.forms | Returns all <form> elements | 1 |
document.head | Returns the <head> element | 3 |
document.images | Returns all <img> elements | 1 |
document.implementation | Returns the DOM implementation | 3 |
document.inputEncoding | Returns the document's encoding (character set) | 3 |
document.lastModified | Returns the date and time the document was updated | 3 |
document.links | Returns all <area> and <a> elements that have a href attribute | 1 |
document.readyState | Returns the (loading) status of the document | 3 |
document.referrer | Returns the URI of the referrer (the linking document) | 1 |
document.scripts | Returns all <script> elements | 3 |
document.strictErrorChecking | Returns if error checking is enforced | 3 |
document.title | Returns the <title> element | 1 |
document.URL | Returns the complete URL of the document | 1 |
Method | |||||
---|---|---|---|---|---|
addEventListener() | 1.0 | 9.0 | 1.0 | 1.0 | 7.0 |
removeEventListener() | 1.0 | 9.0 | 1.0 | 1.0 | 7.0 |
Node | Type | Example |
---|---|---|
ELEMENT_NODE | 1 | <h1 class="heading">W3Schools</h1> |
ATTRIBUTE_NODE | 2 | class = "heading" (deprecated) |
TEXT_NODE | 3 | W3Schools |
COMMENT_NODE | 8 | <!-- This is a comment --> |
DOCUMENT_NODE | 9 | The HTML document itself (the parent of <html>) |
DOCUMENT_TYPE_NODE | 10 | <!Doctype html> |
1 2 3 4 5 6 7 8 9 10 11 12 | JavaScript can be executed in time-intervals. This is called timing events. |
Method | Description |
---|---|
new XMLHttpRequest() | Creates a new XMLHttpRequest object |
abort() | Cancels the current request |
getAllResponseHeaders() | Returns header information |
getResponseHeader() | Returns specific header information |
open(method, url, async, user, psw) | Specifies the requestmethod: the request type GET or POST url: the file location async: true (asynchronous) or false (synchronous)user: optional user namepsw: optional password |
send() | Sends the request to the serverUsed for GET requests |
send(string) | Sends the request to the server.Used for POST requests |
setRequestHeader() | Adds a label/value pair to the header to be sent |
Property | Description |
---|---|
onreadystatechange | Defines a function to be called when the readyState property changes |
readyState | Holds the status of the XMLHttpRequest. 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready |
responseText | Returns the response data as a string |
responseXML | Returns the response data as XML data |
status | Returns the status-number of a request200: "OK"403: "Forbidden"404: "Not Found" For a complete list go to the Http Messages Reference |
statusText | Returns the status-text (e.g. "OK" or "Not Found") |
Method | Description |
---|---|
open(method, url, async) | Specifies the type of request method: the type of request: GET or POST url: the server (file) location async: true (asynchronous) or false (synchronous) |
send() | Sends the request to the server (used for GET) |
send(string) | Sends the request to the server (used for POST) |
Method | Description |
---|---|
setRequestHeader(header, value) | Adds HTTP headers to the request header: specifies the header name value: specifies the header value |
Property | Description |
---|---|
onreadystatechange | Defines a function to be called when the readyState property changes |
readyState | Holds the status of the XMLHttpRequest. 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready |
status | 200: "OK"403: "Forbidden" 404: "Page not found"For a complete list go to the Http Messages Reference |
statusText | Returns the status-text (e.g. "OK" or "Not Found") |
Property | Description |
---|---|
responseText | get the response data as a string |
responseXML | get the response data as XML data |
Method | Description |
---|---|
getResponseHeader() | Returns specific header information from the server resource |
getAllResponseHeaders() | Returns all the header information from the server resource |
JSON.parse()
So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.
Web Browsers Support |
---|
Web Browsers Support |
---|