JavaScript syntax
JavaScript is a programming language. Grammar rules define the structure of a language.
JavaScript Syntax
JavaScript is a scripting language.
It is a lightweight, yet powerful programming language.
JavaScript Literal
In a programming language, a literal is a constant, as in 3.14.
Number literal can be an integer or a decimal, or scientific notation (e).
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = 123e5; </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
String literal You can use single or double quotes:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = 'John Doe'; </script> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
Expression literal is used for calculation:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = 5 * 10; </script> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
##Array ) Literal Define an array:
Object literal Define an object:
Function literal Define a function:
JavaScript VariablesIn programming languages, variables are used to store data values. JavaScript uses the keyword
var to define variables and the equal sign to assign values to variables:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> var length; length = 6; document.getElementById("demo").innerHTML = length; </script> </body> </html>
Run Instance»Click the "Run Instance" button to view the online instance
The variable is a | name. A literal is a value. |
---|
Type | Instance | Description |
---|---|---|
Assignment, arithmetic and bitwise operators | = + - * / | Description in JS operators |
Conditions, comparisons and Logical operators | == != < > | Described in JS comparison operators |
JavaScript Statement
In HTML, a JavaScript statement is a command issued to the browser.
Statements are separated by semicolons:
y = x * 10;
JavaScript keywords
JavaScript statements usually start with keywords. The var keyword tells the browser to create a new variable:
var y = x * 10;
JavaScript Keywords
Like any other programming language, JavaScript reserves some keywords for its own use.
JavaScript also reserves some keywords that are not used in the current language version, but will be used in future JavaScript extensions.
JavaScript keywords must begin with a letter, an underscore (_), or a dollar sign ($).
Following characters can be letters, numbers, underscores or dollar signs (numbers are not allowed to appear as the first character, so that JavaScript can easily distinguish keywords and numbers).
The following are the most important reserved words in JavaScript (in alphabetical order):
abstract | else | instanceof | super |
enum | int | switch | |
export | interface | synchronized | |
let | this | ||
#case | |||
long | throw | ||
#catch | final|||
throws | |||
finally | |||
transient | |||
##class | float | null||
#const | forpackage | ||
function | private | typeof||
#debugger | goto | protectedvar | |
#default | ifpublic | void | |
implements | return | volatile||
#do | import | shortwhile | |
# double | instatic | with | |
In JavaScript, the common naming method is camel case Rules such as lastName (not lastname). |