English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The syntax of JavaScript is a set of rules that define the correct structure of a JavaScript program.
In programming languages, variables are used to store data values.
Variables in standard JavaScript do not have an attached type, and any value can be stored in any variable.
can be usedletfor block-level variables),varorconstto declare variables (for immutable constants).
the equal sign is used to assign a value to a variable.
In this example,numis defined as a variable. Then, fornumAssignment20:
var num; num = 20;Test to see‹/›
JavaScript uses arithmetic operators (+ - * /)to calculate the value.
(20 + 30) * 10Test to see‹/›
JavaScript uses the assignment operator (=) to assign a value to a variable.
var x = 20; var y = 30; var z = x + y;Test to see‹/›
You will learn more about operators in the later part of this tutorial.
The names of variables, functions, or properties in JavaScript are calledIdentifier.
Like any other programming language, JavaScript reserves some identifiers for its own use.
JavaScript also reserves some keywords, which are not used in the current language version but may be used in future JavaScript extensions.
JavaScript identifiers must start with a letter, underscore (_), or dollar sign ($).
The following characters can be letters, numbers, underscores, or dollar signs (numbers are not allowed to be the first character so that JavaScript can easily distinguish identifiers from numbers).
Comments are just a line of text, which is completely ignored by the JavaScript parser.
Comments are usually added to provide additional information related to the source code.
JavaScript supports single-line and multiline comments.
Single-line comments start with double slashes (//)beginning, followed by the comment text.
// document.write("Hello World");Test to see‹/›
while multiline comments start with a slash and an asterisk (/*)beginning, with an asterisk and a slash (*/)end.
/* document.write("Hello World<br>"); document.write("Hello World<br>"); document.write("Hello World<br>"); document.write("Hello World"); */Test to see‹/›
You will learn more about comments in the later part of this tutorial.
All JavaScript identifiers are case-sensitive.
The variables myVariable and myvariable are two different variables:
var myVariable = 1; var myvariable = 2;Test to see‹/›
The convention for JavaScript identifiers is to use camelCase (camel case naming), which means the first word is in lowercase letters, but each subsequent word starts with an uppercase letter.
getElementById(); firstElementChild; textContent; innerHTML;Test to see‹/›
You may also see global variables or constants written in uppercase.
Math.PI;Test to see‹/›
Technically, a complete JavaScript program can be written on one line.
But, this will soon become difficult to read and maintain. Instead, we usually use newlines and indentation for ease of maintenance and readability.
This is a conditional if / Example of an else statement, which can be written on one line or with a newline and indentation.
// Conditional statements written on one line if (x === 1) { /* execute code if true */ } else { /* execute code if false */ } // Indented conditional statements if (x === 1) { // execute code if true } else { // execute code if false }
Note, any code included within a block is indented. Indentation can be done using two spaces, four spaces, or a tab.