Basics of Java Script - Programming: Java, JavaScript Coding For Beginners – Learn In A Day (2015)

Programming: Java, JavaScript Coding For Beginners – Learn In A Day (2015)

Chapter 1. Basics of Java Script

Storing data in JavaScript

· Local Storage: The local storage is used to permanently store the data. With this, the data on that page can be accessed anytime. The local storage doesn’t have an expiration date. One can use local storage if they want their data to be reused again and again over the period of time.

· Session Storage: The session storage is not like local storage as it only holds the data for a single session and disposes off the data once the user closes the page. This kind of storage is used on web pages used for booking tickets etc.

Storing Data using Variable Data and Constant Data

The Variables and Constants store the data and values used in JavaScript. They both have their own importance in JavaScript. All the data stored cannot be made constant. Processes like time countdown use the variables and the Constants are used for non changing data.

Variable Data: As the name suggests, this type of data that changes its value over time is called the Variable Data. This type of data is widely used in JavaScript. Variables are dynamic in nature.

Constant Data: The Constant data is something which never changes its value. This data will be constant through time. This type of data is static. We use this type of data for data which is which cannot be modified.

Creating Variables and Constants

Declaring a Variable

A variable is declared by preceding the name by the var keyword. For example if you want to declare ‘group’ a variable, just add the var keyword before ‘group’. Every variable should end with a ‘ ; ‘ (semicolon). This makes the statement a self containing. Care should be taken while using the words because JavaScript is case sensitive. For this the letters are to be used in the exact same declaration as in the original.

Eg: var Dell;

The object Dell above is declared as a variable.

Declaring a Constant

The keyword ‘const’ is used before the name is to make it a constant. This declaration is just like declaring a constant.

Eg: const Book;

The Book is made a constant in the above example.

Decision making in JavaScript

There always rises a situation where we have to select one of the two paths which are before him. Then we are able to implement the conditional statements like if and else.

The if and else statements can be used for decision making. This will allow the program to choose the correct path.

The following conditional statements are supported by JavaScript.

1. if statement

2. if...else statement

3. if...else if... statement

if statement: The if is the fundamental statement used to make decisions. There a condition is added to the if statement and a set of statements can be given if the given condition holds true.

SYNTAX: if(condition){

statements to be executed}

EXAMPLE:

<script type="text/javascript">
<!--
var age = 20;
if( age > 18 ){
document.write("<b>Allowed inside the pub.</b>");
}
//-->
</script>

OUTPUT: Allowed inside the pub. (if a no. above 18 is given)

if else statement: This is like an extension of the if statement. The system runs the code and executes the statements in the else block if the “if” statement holds false.

SYNTAX:

if(condition)

{statements}

else{statements}

EXAMPLE:

<script type="text/javascript">
<!--
var age = 15;
if( age > 18 ){
document.write("<b>Allowed inside the pub</b>");
}else{
document.write("<b>Is not allowed.</b>");
}
//-->
</script>

OUTPUT:

Allowed inside the pub (if a number above 18 is given)

Not allowed (if a number below 18 is given)

if else if: This is like adding another step to the if else statement. this is used when a series of conditions are used.

SYNTAX: if(condition){statements};

else if(condition){statements}

else if(condition){statements}

else{statements}

EXAMPLE:

<script type="text/javascript">
<!--
var book = "JAVASCRIPT";
if( book == "JAVA" ){
document.write("<b>JAVA Book</b>");
}else if( book == "JAVASCRIPT" ){
document.write("<b>JAVASCRIPT Book</b>");
}else if( book == "DBMS" ){
document.write("<b>DBMS Book</b>");
}else{
��document.write("<b>UNKNOWN Book</b>");
}
//-->
</script>

OUTPUT: JAVASCRIPT book

The nested if

The ‘nested if’ is an ‘if’ statement inside another ‘if’ statement.

SYNTAX: if(condition) {if(condition){statement} }

The switch and break statements:

In the switch statement there will be an expression followed by several different statements. The statements will be executed basing on the expression’s value selected.

SYNTAX:switch (expression)
{
case condition 1
: statement(s)
break;
case condition 2: statement(s)
break;
.

.

,
case condition n: statement(s)
break;
default:
statement(s)
}

The break statement is used to indicate that it is the end of the preceding switch case.

Looping in JavaScript

The for Loop:

The loops are used if one wants the same code to be repeat again and again. The ‘for’ loop is one of the three loops which can be made use of. The ‘for loop’ is simple and compact.

It contains the three following points.

1. The start statement is performed before the loop begins.

2. The loop is executed only when the conditions given are true. But a test code will be executed which runs the code for once.

3. The counter for iterations can be increased or decreased.

SYNTAX:

for (initialization; test condition; iteration statement){
Statement(s) to be executed if test condition is true

}

EXAMPLE:

<script type="text/javascript">
<!--
var count;
document.write("This will repeat for 10 times" + "<br />");
for(count = 0; count < 10; count++){
document.write("Currently repeated : " + count );
document.write("<br />");
}
document.write("Done!!");
//-->
</script>

OUTPUT:

This will repeat 10 times

Currently repeated : 1

Currently repeated : 2

Currently repeated : 3

Currently repeated : 4

Currently repeated : 5

Currently repeated : 6

Currently repeated : 7

Currently repeated : 8

Currently repeated : 9

Currently repeated : 10