JavaScript Statements
JavaScript programs
A computer program is a list of ‘’instructions’’ to be ‘’executed’’ by a computer.
In a programming language, these programming instructions are called statements.
A JavaScript program is a list of programming statements.
JavaScript statements are composed of:
o values
o operators
o expressions
o keywords
o comments
Most JavaScript programs contain many JavaScript statements.
The statements are executed, one by one, in the same order as they are written
Semicolons (;)
Semicolons separate JavaScript statements
Add a semicolon at the end of each executable statement
When separated by semicolons, multiple statements on one line are allowed
Ending statements with semicolon is not required, but highly recommended
JavaScript White Space
JavaScript ignores multiple spaces
You can add white space to your script to make it more readable
A good practice is to put spaces around operators ( = + - * / )
JavaScript Line Length and Line Breaks
For best readability, programmers often like to avoid code lines longer than 80 characters
If a JavaScript statement does not fit on one line, the best place to break it is after an
operator ( = + - * / )
JavaScript Code Blocks
JavaScript statements can be grouped together in code blocks, inside curly brackets {…}
The purpose of code blocks is to define statements to be executed together
One place you will find statements grouped together in blocks is in JavaScript functions
JavaScript Keywords
JavaScript statements often start with a keyword to identify the JavaScript action to be
performed
Some of the keywords are the following:
o var
declares a variable
o let
declares a block variable
o const
declares a block constant
o function
declares a function
o return
exits a function
JavaScript keywords are reserved words
Reserved words cannot be used as names for variables
JavaScript Syntax
JavaScript Syntax is the set of rules, how JavaScript programs are constructed
JavaScript Values
The JavaScript syntax defines two types of values:
o fixed values they are called literals
o variable values they are called variables
JavaScript Literals
The two most important syntax rules for fixed values are:
o Numbers are written with or without decimals
o Strings are text, written within double or single quotes
JavaScript Variables
In a programming language, variables are used to store data values
JavaScript uses the keywords var, let and const to declare variables
An equal sign ( = ) is used to assign values to variables
In this example, x is defined as a variable.
Then, x is assigned the value 6.
JavaScript Operators
JavaScript uses arithmetic operators ( + - * / ) to compute values
JavaScript Expressions
An expression is a combination of values, variables, and operators, which computes to a
value
The computation is called an evaluation
JavaScript Keywords
JavaScript keywords are used to identify actions to be performed
The let keyword tells the browser to create variables
The var keyword also tells the browser to create variables
JavaScript Comments
Not all JavaScript statements are ‘’executed’’
Code after double slashes // or between /* and */ is treated as a comment
Comments are ignored, and will not be executed
JavaScript Identifiers/Names
Identifiers are JavaScript names
Identifiers are used to name variables and keywords, and functions
A JavaScript name must begin with:
o a letter ( A-Z or a-z )
o a dollar sign ( $ )
o an underscore ( _ )
Subsequent characters may be letters, digits, underscores or dollar signs.
Numbers are not allowed as first character in names.
JavaScript
All JavaScript identifiers are case sensitive
o for example, the variables lastName and lastname, are two different variables
JavaScript and Camel Case
Historically, programmers have used different ways of joining multiple words into one
variable name:
o underscore (e.g. first_name)
o upper camel case (e.g. FirstName)
o lower camel case (e.g. firstName)
JavaScript Character Set
JavaScript uses the Unicode character set
o Unicode is a way to represent the characters of all the languages in the world
o Unicode provides a unique number for every character, including punctuation
marks, mathematical symbols and characters making up non-Latin alphabets such
as Thai, Chinese or Arabic script
o Unicode is maintained by the Unicode Consortium, a non-profit organization that
exists to develop and promote the Unicode2