JavaScript Programming Structure

computer program is a list of “instructions” to be “executed” by a computer.

In a programming language, these programming instructions are known as statements.

JavaScript program is a list of programming statements.

In HTML, JavaScript codes are executed and run by the web browser.

The first thing we’ll study is the building blocks of code.


Statements

JavaScript statements are composed of:

Values, Operators, Expressions, Keywords, and Comments.

We’ve already seen a statement, alert('Hello, world!'), which shows the message “Hello, world!”.

We can have as many statements in our code as we want.

It is terminated with a semicolon.

For example,

Wrong Method: Here “Hello World” are given into two statements alert:

alert('Hello'); alert('World');

Correct Usage: Usually, statements are placed on separate next lines to make the code more easy readable:

alert('Hello');
alert('World');

JavaScript White Space

JavaScript ignores multiple spaces. You can add white space to your script to make it more readable.

The following lines are equivalent:

var person = "Hege";
var person="Hege";

A good practice is to put spaces around operators ( = + – * / ):

var x = y + z; 


JavaScript Keywords

JavaScript statements often start with a keyword to identify the JavaScript action to be performed.

KeywordDescription
breakTerminates a switch or a loop
continueJumps out of a loop and starts at the top
debuggerStops the execution of JavaScript, and calls (if available) the debugging function
do … whileExecutes a block of statements, and repeats the block, while a condition is true
forMarks a block of statements to be executed, as long as a condition is true
functionDeclares a function
if … elseMarks a block of statements to be executed, depending on a condition
returnExits a function
switchMarks a block of statements to be executed, depending on different cases
try … catchImplements error handling to a block of statements
varDeclares a variable

JavaScript keywords are also known to be reserved words. Reserved words cannot be used as names for variables.

Leave a Reply

Your email address will not be published. Required fields are marked *