MODULE - 1
STATEMENTS
A script is a series of instructions that a computer can follow one-by-one.
Each individual instruction or step is known as a statement.
Statements should end with a semicolon.
We will look at what the code on the right does var today = new Date();
shortly, but for the moment note that: var hourNow = [Link]();
var greeting;
Each of the lines of code in green isastatement.
if (hourNow > 18) {
The pink curly braces indicate the start and end
of a code block. (Each code block could contain
greeting = 'Good evening';
many more statements.) } else if (hourNow > 12) {
greeting = 'Good afternoon';
The code in purple determines which code } else if (hourNow > 0) {
should run (as you will see on p149).
greeting = 'Good morning';
} else {
JAVASCRIPT IS CASE SENSITIVE greeting 'Welcome';=
}
JavaScript is case sensitive so hourNow means
[Link](greeting);
something different to HourNow or HOURNOW.
STATEMENTS ARE INSTRUCTIONS AND STATEMENTS CAN BE ORGANIZED
EACH ONE STARTS ON A NEW LINE INTO CODE BLOCKS
A statement is an individual instruction that the Some statements are surrounded by curly braces;
computer should follow. Each one should start on a these are known as code blocks. The closing curly
new line and end with a semicolon. This makes your brace is not followed by a semicolon.
code easier to read and follow.
Above, each code block contains one statement
The semicolon also tells the JavaScript interpreter related to what the current time is. Code blocks
when a step is over, indicating that it should move will often be used to group together many more
to the next step. statements. This helps programmers organize their
code and makes it more readable.
56 BASIC JAVASCRIPT INSTRUCTIONS
COMMENTS
You should write comments to explain what your code does.
They help make your code easier to read and understand.
This can help you and others who read your code.
/* This script displays a greeting to the user based upon the current time.
It is an example from JavaScript & jQuery book */
var today = new Date(); // Create a new date object
var hourNow = [Link](); // Find the current hour
var greeting;
// Display the appropriate greeting based on the current time
if (hourNow > 18) {
greeting = 'Good evening';
) else if (hourNow > 12) {
greeting = 'Good afternoon';
} else if (hourNow > 0) {
greeting = 'Good morning';
} else{
greeting = 'Welcome';
JavaScript code is green
[Link](greeting); Multi-line comments are pink
Single-line comments are gray
MULTI-LINE COMMENTS SINGLE-LINE COMMENTS
To write a comment that stretches over more than In a single-line comment, anything that follows the
one line,you use a multi-line comment, starting with two forward slash characters // on that line will not
the/*characters and ending with the*/characters. be processed by the JavaScript interpreter. Single-
Anything between these characters is not processed line comments are often used for short descriptions
by the JavaScript interpreter. of what the code is doing.
Multi-line comments are often used for descriptions Good use of comments will help you if you come
of how the script works, or to prevent a section of back to your code after several days or months.
the script from running when testing it. They also help those who are new to your code.
BASIC JAVASCRIPT INSTRUCTIONS 57