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.


JavaScript Literals

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

Number Literals can be integers, decimals, or expressed in scientific notation (e):

3.14
1001
123e5

String Literals can be written with 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 var keyword to define variables and the assignment operator (=) to assign values:

var x, lengthx = 5
length = 6

Variables can be accessed by their names.
In imperative languages, variables are usually mutable, while literals represent constant values.

Note | A variable is a name, while a literal is a value.

JavaScript Operators

JavaScript uses arithmetic operators to perform calculations:

(5 + 6) * 10

JavaScript uses assignment operators to assign values to variables:

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

JavaScript has several types of operators:

TypeExampleDescription
Assignment, Arithmetic, and Bitwise Operators=, +, -, *, /Described in JS operators section
Conditional, Comparison, and Logical Operators==, !=, <, >Described in JS comparison operators section

JavaScript Statements

In HTML, JavaScript statements are commands sent to the browser.

Statements are separated by semicolons:

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

JavaScript Keywords

JavaScript keywords are used to identify the actions to be performed.

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

The var keyword tells the browser to create a new variable:

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

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

Here are the most important reserved words in JavaScript (in alphabetical order):

abstractelseinstanceofsuper
booleanenumintswitch
breakexportinterfacesynchronized
byteextendsletthis
casefalselongthrow
catchfinalnativethrows
charfinallynewtransient
classfloatnulltrue
constforpackagetry
continuefunctionprivatetypeof
debuggergotoprotectedvar
defaultifpublicvoid
deleteimplementsreturnvolatile
doimportshortwhile
doubleinstaticwith

JavaScript Comments

Not all JavaScript statements are "commands."
Anything following a double slash // will be ignored by the browser:

// This will not execute

JavaScript Data Types

JavaScript has multiple data types: numbers, strings, arrays, objects, and more:

var length = 16;                                  // Number assigned via number literal
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 very important.

Understanding data types is essential to work with variables effectively.

Without using data types, the following example would not work:

16 + "Volvo"

What happens if you add 16 to "Volvo"?

Will the above produce an error, or will it output the following result?

"16Volvo"

You can try running the above code in your browser to see the result.

In the following chapters, you will learn more about data types.


JavaScript Functions

JavaScript statements can be placed inside functions, and functions can be reused:

Referencing a function = calling the function (executing the statements inside the function).

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, make sure the Caps Lock key is off.

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 marks.


Did You Know?

Note
In JavaScript, the common naming convention is camelCase, such as lastName (instead of lastname).

Published on September 13, 2025