JavaScript Syntax
JavaScript is a programming language. Syntax rules define the language structure.
JavaScript Syntax
JavaScript is a scripting language.
It is a lightweight but powerful programming language.
JavaScript Literals
In programming languages, fixed values are generally called literals, such as 3.14.
Number LiteralsThey can be integers or decimals, or scientific notation (e).
String LiteralsYou can use single or double quotes:
Expression LiteralsUsed for calculation:
Array LiteralsDefine an array:
Object LiteralsDefine an object:
Function LiteralsDefine a function:
JavaScript Variables
In programming languages, variables are used to store data values.
JavaScript uses keywordsvarto define variables, and use the equals sign to assign values to variables:
Variables can be accessed by variable name. In imperative languages, variables are usually mutable. A literal is a constant value.
![]() |
A variable is aname.A literal is aValue。 |
|---|
JavaScript Operators
JavaScript usesArithmetic Operatorsto calculate values:
JavaScript usesAssignment Operatorsto assign values to variables:
The JavaScript language has many types of operators:
| Type | Examples | Description |
|---|---|---|
| Assignment, Arithmetic, and Bitwise Operators | = + - * / | Described in JS Operators |
| Conditional, Comparison, and Logical Operators | == != < > | Described in JS Comparison Operators |
JavaScript Statements
In HTML, JavaScript statements are used to send commands to the browser.
Statements are separated by semicolons:
y = x * 10;
JavaScript Keywords
JavaScript keywords are used to identify operations to be performed.
Like any other programming language, JavaScript reserves some keywords for its own use.
varThe keyword tells the browser to create a new variable:
var y = x * 10;
JavaScript also reserves some keywords that are not used in the current language version but will be used in future JavaScript extensions.
The following are the most important reserved keywords in JavaScript (in alphabetical order):
| abstract | else | instanceof | super |
| boolean | enum | int | switch |
| break | export | interface | synchronized |
| byte | extends | let | this |
| case | false | long | throw |
| catch | final | native | throws |
| char | finally | new | transient |
| class | float | null | true |
| const | for | package | try |
| continue | function | private | typeof |
| debugger | goto | protected | var |
| default | if | public | void |
| delete | implements | return | volatile |
| do | import | short | while |
| double | in | static | with |
JavaScript Comments
Not all JavaScript statements are "commands". Double slash//The content after the double slash will be ignored by the browser:
JavaScript Data Types
JavaScript has several data types: numbers, strings, arrays, objects, etc.:
var points = x * 10; // Number assigned via expression literal
var lastName = "Johnson"; // String assigned via string literal
var cars = ["Saab", "Volvo", "BMW"]; // Array assigned via array literal
var person = {firstName:"John", lastName:"Doe"}; // Object assigned via object literal
Concept of Data Types
In programming languages, data types are a very important concept.
In order to operate on variables, it is very important to understand the concept of data types.
Without data types, the following example cannot be executed:
How is 16 plus "Volvo" calculated? Will the above produce an error or output the following result?
You can try executing the above code in your browser to see the effect.
In the following chapters, you will learn more about data types.
JavaScript Functions
JavaScript statements can be written inside functions, and functions can be referenced repeatedly:
Referencing a function= calling the function (executing the statements inside the function).
return a * b; // Returns the result of a multiplied by b
}
JavaScript Case Sensitivity
JavaScript is case-sensitive.
When writing JavaScript statements, please be mindful of whether the caps lock key is turned off.
FunctionsgetElementByIdandgetElementbyIDare different.
Similarly, variablesmyVariableandMyVariableare also different.
JavaScript Character Set
JavaScript uses the Unicode character set.
Unicode covers all characters, including punctuation and other characters.
For more information, please study ourComplete Unicode Reference Manual。
Did you know?
![]() |
In JavaScript, camel case naming convention is common, such as lastName (rather than lastname). |
|---|
