JS Code

This section will deal with Statements, Syntax, Comments, Variables, Operators and Arithmetic. You can do eveything in JavaScript that you can do in Python, but there are different rules. One of the key things in JS is that every line of code ends with a semi colon. Try not to forget;

Statements in JS can have many different styles. Here are 4 of the common types:

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. In HTML, JavaScript programs are executed by the web browser.

JavaScript Statements

JavaScript statements are composed of:

This statement tells the browser to write "Hello World!" inside an HTML element with id="greeting":

Most JavaScript programs contain many JavaScript statements. The statements are executed, one by one, in the same order as they are written. JavaScript programs (and JavaScript statements) are often called JavaScript code.

Semicolons ;

Semicolons " ; " are key in JS and typically are used at the end of every statement. They can be frustrating and are easy to forget, but try to think of them as full stops and put them at the end of every sentence. Semicolons separate JavaScript statements.

Assigning Variables

Method 1

Add a semicolon at the end of each executable statement as follows (see the code on lines 104-108):

Method 2

When separated by semicolons, multiple statements on one line are allowed (see the code on lines 122-124):

JavaScript Statements

JavaScript code blocks are written between { and }. These are called curley braces and are used in sets in maths

Click below for a beautiful quote that highlights the trouble with conventions in creating coding languages.

JavaScript Keywords

JavaScript statements often start with a keyword to identify the JavaScript action to be performed. To view a full list of JavaScript keywords visit:

JavaScript Reserved Keywords

Here is a list of some of the keywords you 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
JavaScript keywords are reserved words. Reserved words cannot be used as names for variables.

Block Quotes

In Python we can use """ and """ to 'comment out' a block of code. This is a handy trick and JS has a similar commenting style. To open the block use /* and then close with */. Notice the nice symmetry.