JavaScript Syntax
Syntax Rules
Rules how Programs Must be Constructed
let x = 5;
let y = 6;
// How to Compute values:
let z = x + y;
// I am a Comment. I do Nothing
JavaScript Values
The JavaScript syntax defines two types of values:
- Literals (Fixed values)
- Variables (Variable values)
JavaScript Literals
The most important syntax rules for literals (fixed values) are:
Numbers are written with or without decimals:
1001
Strings are text, written within double or single quotes:
'John Doe'
JavaScript Variables
Variables are containers for storing data values.
Variables must be identified with unique names.
JavaScript Identifiers
Identifiers are used to name variables and keywords, and functions.
The rules for legal names are the same in most programming languages:
A name must begin with | A name can contain |
---|---|
A letter (A-Z or a-z) | A letter (A-Z or a-z) |
A number (0-9) | |
A dollar sign ($) | A dollar sign ($) |
An underscore (_) | An underscore (_) |
Note
Numbers are not allowed as the first character in names.
This way JavaScript can easily distinguish identifiers from numbers.
JavaScript Keywords
JavaScript keywords are used to defines actions to be performed.
The
let
and const
keywords create variables:
JavaScript Operators
JavaScript assignment operators (=) assign values to variables:
JavaScript uses arithmetic operators ( +
-
*
/
) to
compute values:
JavaScript Expressions
An expression is a combination of values, variables, and operators, which computes to a value.
Examples
(5 + 6) * 10 evaluates ti 110:
Expressions can also contain variable:
"John" + " " + "Doe", evaluates to "John Doe":
JavaScript Comments
Code after double slashes
//
or between /*
and */
is treated as a comment.
Comments are ignored, and will not be executed:
Note
You will learn more about comments in a later chapter.
JavaScript is Case Sensitive
All JavaScript identifiers are case sensitive.
The variables lastName
and lastname
,
are two different variables:
Note
JavaScript does not interpret LET or Let as the keyword let.
JavaScript and Camel Case
Historically, programmers have used different ways of joining multiple words into one variable name:
Hyphens:
first-name, last-name, master-card, inter-city.
Note
Hyphens are not allowed in JavaScript. They are reserved for subtractions.
Underscore:
first_name, last_name, master_card, inter_city.
Upper Camel Case (Pascal Case):
FirstName, LastName, MasterCard, InterCity.
Lower Camel Case:
JavaScript programmers tend to use camel case that starts with a lowercase letter:
firstName, lastName, masterCard, interCity.