Welcome to the Javascript Statements Homepage, Guest!


Javascript | Statements, instructions that will be executed




Statements

The web browser is responsible for executing the Javascript statements present inside the HTML document or linked javascript file. Javascript statements are similar to the following:

document.getElementById("tutorial").innerHTML = "Hello world, How are you today?"

The statement tells the browser to write Hello world, How are you today? inside the HTML element with id set to tutorial. Javascript programs and statements are often considered to be called javascript code. Most of the javascript programs may contain multiple javascript statements. The statements are read in the order they are written, just like a file written in C++, C, Java, or Python would be. In the example below, the variables created are given specific values, and displayed using the variable z:

var x, y, z;
x = 10;
y = 5;
z = x + y;
document.getElementById("tutorial").innerHTML = z;

The value stored in the element with id tutorial will be replaced by the value assigned to z, which is 15, and updated dynamically.




Semicolons

You should notice that each line of javascript code ends in a semicolon. You don't have to separate the lines out into many either. The following line of code is perfectly acceptable in the Javascript rules, but I prefer to space them out. So, I can read them easier, and I can comment accordingly without having to refer to just one line of code for multiple commands. It is useful for declaring a bunch of variables all at one time, like the example below, but ending a javascript line with a semicolon is not necessarily required. The semicolon is not necessarily required for the code to function, but the use of a semicolon is highly recommended from just about everyone who codes with Javascript. There are too many issues that could arise when not using a semicolon. So, just use one and comment.

a = 10; b = 20; c = 30; d = a+b*c;

Javascript will ignore multiple white spaces. You can add more white space to your script files, and javascript will still read the code perfectly fine. The following lines of code are equal to each other under the rules of Javascript:

var myName = "James";
var myName="James";

A good technique to adhere to is to put white space around operators, such as =, +, -, *, and /. The technique will help you read the code easier. The white space will allow the user to understand what is going on, and will also give you room to think. The code may take up more room, but lengthy code is not always a bad thing. I would rather have 10 short lines of code that can be read easily, instead of 5 longer and more condensed lines of code.

var myName = "James"; var myName="James";



Line Breaks

For the best readability, the programmer should avoid lines of code that are longer than 80 characters in length. The longer the code is, the harder the code will be to read overall. The line of code should really not be any longer than the typical length of a line spanning your screen. If you do have to cut a line, the best way to do so is at the operator:

document.getElementById("").innerHTML =
"Hello world";



Code Blocks

Javascript's statements can be grouped together in code blocks, like a function, encased in curly brackets. The purpose of the code block is to define a group of statements to be executed together. The code blocks will have one to multiple Javascript statements together in one place. Good programming techniques say we should not put too many statements into one function. You should break the code into functions, and limit each function to one specific task.

function myFunctionThatWillExecute()
{
     document.getElementById("tutorial1").innerHTML = "Hello World, ";
     document.getElementById("tutorial2").innerHTML = "how are you today? ";
}


Functions will return later on in this tutorial, but for now let us finish the statements page up with more specific keywords.




Keywords

Javascript statements will often begin with a reserved keyword to identify the action to be performed by the web browser. A list of a few specific keywords are below:

  • break will terminate a switch or a loop

  • continue will jump out of a loop and start back at the top

  • debugger stops execution of JS code, and will call the debugger if possible

  • do .. while will repeat a block of code while the conditional statement returns true

  • for is reserved for specific for loops

  • function will declare the use of a function inside Javascript

  • if .. else are reserved for if then else statements inside Javascript code

  • return exits a function

  • switch will execute a block of code depending on the conditions present

  • try .. catch are blocks of code used to implement error handling for Javascript statements

  • var is a reserved word that will declare a variable




Next, we will take a look at comments in Javascript more closely.

Just follow the buttons to continue the tutorial, Guest