Easy Tutorial
❮ Jsref Regexp G Js Obj Math ❯

JavaScript Syntax


JavaScript is a programming language. Syntax rules define the structure of the language.


JavaScript Syntax

JavaScript is a scripting language.

It is lightweight yet powerful in terms of programming capabilities.


JavaScript Literals

In programming languages, fixed values are called literals, such as 3.14.

Number Literals can be integers or decimals, or even scientific notation (e).

3.14
1001
123e5

String Literals can use single or double quotes:

"John Doe"
'John Doe'

Expression Literals are used for calculations:

5 + 6
5 * 10

Array Literals define an array:

[40, 100, 1, 5, 25, 10]

Object Literals define an object:

{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}

Function Literals define a function:

function myFunction(a, b) { return a * b;}

JavaScript Variables

In programming languages, variables are used to store data values.

JavaScript uses the keyword var to define variables and an equal sign to assign values to variables:

var x, length;
x = 5;
length = 6;

Variables can be accessed by their names. In imperative languages, variables are usually mutable. Literals are a constant value.

| | Variable is a name. Literal is a value. | | --- | --- |


JavaScript Operators

JavaScript uses arithmetic operators to compute values:

(5 + 6) * 10

JavaScript uses assignment operators to assign values to variables:

x = 5;
y = 6;
z = (x + y) * 10;

JavaScript has various types of operators:

Type Example 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 commands to the browser.

Statements are separated by semicolons:

x = 5 + 6;
y = x * 10;

JavaScript Keywords

JavaScript keywords are used to identify actions to be performed.

Like any programming language, JavaScript reserves some keywords for its own use.

var x = 5 + 6;
var y = x * 10;

JavaScript also reserves some keywords that are not used in the current version but may be used in future extensions.

Here are some of the most important reserved keywords in JavaScript (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." Anything after double slashes // will be ignored by the browser:

// This won't be executed

JavaScript Data Types

JavaScript has various data types: numbers, strings, arrays, objects, etc.:

var length = 16;                                  // Number by numeric literal
var points = x * 10;                               // Number by expression literal
var lastName = "Johnson";                          // String by string literal
var cars = ["Saab", "Volvo", "BMW"];               // Array by array literal
var person = {firstName:"John", lastName:"Doe"};   // Object by object literal

Concept of Data Types

In programming languages, data types are a crucial concept.

Understanding data types is important to manipulate variables effectively.

Without data types, the following example would not work:

16 + "Volvo"

Would adding 16 and "Volvo" result in an error or output the following?

"16Volvo"

You can try executing the above code in your browser to see the effect.

More on data types will be covered in the following sections.


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 it).

function myFunction(a, b) {
    return a * b;         
    // Returns the result of a multiplied by b
}

JavaScript Case Sensitivity

JavaScript is case-sensitive.

When writing JavaScript statements, pay attention to the case sensitivity.

The function getElementById is different from getElementbyID.

Similarly, the variable myVariable is different from MyVariable.


JavaScript Character Set

JavaScript uses the Unicode character set.

Unicode covers all characters, including punctuation and other symbols.

For more details, refer to our Complete Unicode Reference.


Did You Know?

| <br> | In JavaScript, the common naming convention is camelCase, such as lastName (not lastname). | | --- | --- |

❮ Jsref Regexp G Js Obj Math ❯