A computer program is a list for a computer to "run" the instructions. These instructions are referred to as statements in a programming language.
Example:
Note: In HTML, JavaScript programs are executed by the web browser.
Output:
A Java Script program is a list of computer statements.
The value of z is 8.
The declarations in Java Script include: values, operators, expressions, keywords and commentary. This statement says to the browser "Hello UK" to write "id="demo" inside a HTML element:
Example:
Most Java Script programs contain many Java Script statements. The declarations are performed in the same order as they are written one by one.
Note: Java Script programs (and Java Script statements) are often called Java Script code.
Output:
In HTML, Java Script statements are executed by the browser.
Hello UK.
Semicolons separate Java Script statements. Add a semicolon at the end of each executable statement:
Example:
Output:
Java Script statements are separated by semicolons.
11
When separated by semicolons, multiple statements on one line are allowed:
Example:
Note: Without semicolons, we can see examples in the web. Semicolon termination statements are not necessary but strongly recommended.
Output:
Multiple statements on one line is allowed.
11
Java Script ignores multiple spaces. To make your script more readable, we can add white space to it.
The following lines are equivalent:
Example:
A good practice is to put spaces around operators ( = + - * / ):
For best readability, programmers often like to avoid code lines longer than 80 characters. If a Java Script statement does not fit on one line, the best place to break it is after an operator:
Example:
Output:
Java Script code blocks are written between { and }
Note: In this tutorial we use 4 spaces of indentation for code blocks.
Java Script statements often start with a keyword to identify the Java Script action to be performed. Here is a list of some of the keywords we will learn about in this tutorial:
Keyword |
Description |
break | Terminates a switch or a loop |
continue | Jumps out of a loop and starts at the top |
debugger | Stops the execution of JavaScript, and calls (if available) the debugging function |
do ... while | Executes a block of statements, and repeats the block, while a condition is true |
for | Marks a block of statements to be executed, as long as a condition is true |
function | Declares a function |
if ... else | Marks a block of statements to be executed, depending on a condition |
return | Exits a function |
switch | Marks a block of statements to be executed, depending on different cases |
try ... | catch Implements error handling to a block of statements |
var | Declares a variable |
Note: JavaScript keywords are reserved words. Unable to use reserved words as variable names.
We use cookies to ensure you get the best experience on our website, if you continue to browse you'll be acconsent with our Cookies Policy.
Got it!