JavaScript syntax refers to the rules and guidelines that define how JavaScript programs are written and structured.
// Declaring variables:
let a;
let b;
// Assigning values to variables:
a = 10;
b = 15;
// Declaring and assigning a variable in one step:
let sum = a + b;
JavaScript Syntax Categories
The JavaScript syntax categorizes values into two types:
- Fixed values, also known as Literals.
- Changeable values, also known as Variables.
JavaScript Literals
The two key syntax rules for fixed values are:
- Numbers can be written with or without decimal points.
10.50
1001
- Strings are text enclosed in either double quotes (") or single quotes (').
"William Wilson"
'William Wilson'
JavaScript Variables
Variables store data values and can be declared using the let
, const
, or var
keywords. The key difference is that const
creates a constant variable, which cannot be reassigned.
let x = 10; // Variable with `let` (can be reassigned)
const y = 20; // Constant variable with `const` (cannot be reassigned)
JavaScript Operators
JavaScript uses arithmetic operators (+
, -
, *
, /
) to perform calculations and compute values.
(5 + 6) * 10
The assignment operator (=
) is used to set a value to a variable.
let x, y;
x = 5;
y = 6;
JavaScript Expressions
An expression is a combination of values, variables, and operators that results in a computed value.
5 * 10 // Evaluates to 50
Expressions can also include values stored in variables.
x * 10
Values can be of different types, including numbers and strings.
For example, the expression "Alice" +
" " +
"Smith" evaluates to "Alice Smith".
"Alice" + " " + "Smith" // + is a concatination operator for strings
JavaScript Keywords
JavaScript keywords are used to define actions that the program should carry out.
The let
keyword instructs the browser to create variables.
let a, b;
a = 10 + 5;
b = a * 5;
document.getElementById("key").innerHTML = b;
/*
Output:
75
*/
The var
keyword is also used to instruct the browser to create variables.
var a, b;
a = 10 + 5;
b = a * 5;
document.getElementById("key").innerHTML = b;
/*
Output:
75
*/
JavaScript Comments
Text following double slashes (//
) or enclosed between /*
and */
is treated as a comment.
Comments are ignored by the JavaScript engine and do not execute.
let a;
a = 10;
// a = 5; It will not be executed
document.getElementById("comment").innerHTML = a;
JavaScript Identifiers
Identifiers are the names used in JavaScript to label variables, keywords, and functions.
A JavaScript identifier must start with one of the following:
- A letter (A-Z or a-z)
- A dollar sign ($)
- An underscore (_)
After the first character, it can include letters, digits (0-9), underscores, or dollar signs.
Note: Identifiers cannot begin with a number. This ensures JavaScript can easily differentiate between identifiers and numeric values.
JavaScript is Case Sensitive
JavaScript identifiers are case-sensitive.
For example, firstname
and FirstName
are considered two distinct variables.
let firstname, FirstName;
firstname = "William";
FirstName = "James";
document.getElementById("sensitive").innerHTML = firstname;
/*
Output:
William
*/
JavaScript treats LET and Let differently from the keyword let
, as it is case-sensitive.