Tuesday, 14 October 2014

JavaScript Operators

JavaScript uses arithmetic operators to compute values (just like algebra):
(5 + 6) * 10
Try it Yourself »
JavaScript uses an assignment operator to assign values to variables (just like algebra):
x = 5;
y = 6;
z = (x + y) * 10;
Try it Yourself »





JavaScript Statements

A computer program is a sequences of "executable commands" called statements.
JavaScript statements are separated by semicolon:
x = 5 + 6;
y = x * 10;
Multiple statements on one line is allowed:
x = 5 + 6; y = x * 10;












 JavaScript Keywords

A JavaScript statement often starts with a keyword.
The var keyword tells the browser to create a new variable:
var x = 5 + 6;
var y = x * 10

JavaScript Data Types

JavaScript variables can hold many data types: numbers, text strings, arrays, objects and more:
var length = 16;                               // Number assigned by a number literal var points = x * 10;                           // Number assigned by an expression literal var lastName = "Johnson";                      // String assigned by a string literal var cars = ["Saab", "Volvo", "BMW"];           // Array  assigned by an array literalvar x = {firstName:"John", lastName:"Doe"};    // Object assigned by an object literal

  JavaScript Functions

JavaScript statements written inside a function, can be invoked and reused many times.
Invoke a function = call a function (ask for the code in the function to be executed).
function myFunction(a, b) {
    return a * b;                   // return the product of a and b


 



JavaScript Statement

In HTML, JavaScript statements are "command lines" executed by the web browser.

In HTML, JavaScript statements are "commands" to the browser.
The purpose, of the statements, is to tell the browser what to do.
This statement tells the browser to write "Hello Dolly" inside an HTML element identified with id="demo":

Example

document.getElementById("demo").innerHTML = "Hello Dolly.";

Try it Yourself »

 JavaScript Code

JavaScript code (or just JavaScript) is a sequence of JavaScript statements.
Each statement is executed by the browser in the sequence they are written.
This example will manipulate two different HTML elements:

Example

document.getElementById("demo").innerHTML = "Hello Dolly.";
document.getElementById("myDiv").innerHTML = "How are you?";

Try it Yourself »


Semicolon ;

Semicolon separates JavaScript statements.
Normally you add a semicolon at the end of each executable statement:
a = 5;
b = 6;
c = a + b;

Try it Yourself »
N/B 
You might see examples without semicolons.
Ending statements with semicolon is optional in JavaScript.

 JavaScript Code Blocks

JavaScript statements can be grouped together in blocks.
Blocks start with a left curly bracket, and end with a right curly bracket.
The purpose of a block is to make the sequence of statements execute together.
A good example of statements grouped together in blocks, are in JavaScript functions.
This example will run a function that will manipulate two HTML elements:

Example

function myFunction() {
    document.getElementById("demo").innerHTML = "Hello Dolly.";
    document.getElementById("myDIV").innerHTML = "How are you?";
}

Try it Yourself »
 

JavaScript Statement Identifiers

JavaScript statements often start with a statement identifier to identify the JavaScript action to be performed.
Statement identifiers are reserved words and cannot be used as variable names (or any other things).
Here is a list of some of the JavaScript statements (reserved words) you will learn about in this tutorial:
Statement Description
break Terminates a switch or a loop
catch Marks the block of statements to be executed when an error occurs in a try block
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
for ... in Marks a block of statements to be executed, for each element of an object (or array)
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
throw Throws (generates) an error
try Implements error handling to a block of statements
var Declares a variable
while Marks a block of statements to be executed, while a condition is true  

No comments:

Post a Comment