Control structure in javascript in web technology


Introduction

JavaScript is a lightweight, interpreted programming language with object-oriented capabilities that allows you to build interactivity into otherwise static HTML pages.

JavaScript code is not compiled but translated by the translator. This translator is embedded into the browser and is responsible for translating javascript code.

Key Points

  • It is Lightweight, interpreted programming language.

  • It is designed for creating network-centric applications.

  • It is complementary to and integrated with Java.

  • It is complementary to and integrated with HTML

  • It is an open and cross-platform

JavaScript Statements

JavaScript statements are the commands to tell the browser to what action to perform. Statements are separated by semicolon [;].

JavaScript statement constitutes the JavaScript code which is translated by the browser line by line.

Example of JavaScript statement:

document.getElementById["demo"].innerHTML = "Welcome";

Following table shows the various JavaScript Statements −

Sr.No.StatementDescription
1. switch case A block of statements in which execution of code depends upon different cases. The interpreter checks each case against the value of the expression until a match is found. If nothing matches, a default condition will be used.
2. If else The if statement is the fundamental control statement that allows JavaScript to make decisions and execute statements conditionally.
3. While The purpose of a while loop is to execute a statement or code block repeatedly as long as expression is true. Once expression becomes false, the loop will be exited.
4. do while Block of statements that are executed at least once and continues to be executed while condition is true.
5. for Same as while but initialization, condition and increment/decrement is done in the same line.
6. for in This loop is used to loop through an object's properties.
7. continue The continue statement tells the interpreter to immediately start the next iteration of the loop and skip remaining code block.
8. break The break statement is used to exit a loop early, breaking out of the enclosing curly braces.
9. function A function is a group of reusable code which can be called anywhere in your programme. The keyword function is used to declare a function.
10. return Return statement is used to return a value from a function.
11. var Used to declare a variable.
12. try A block of statements on which error handling is implemented.
13. catch A block of statements that are executed when an error occur.
14. throw Used to throw an error.

JavaScript Comments

JavaScript supports both C-style and C++-style comments, thus:

  • Any text between a // and the end of a line is treated as a comment and is ignored by JavaScript.

  • Any text between the characters /* and */ is treated as a comment. This may span multiple lines.

  • JavaScript also recognizes the HTML comment opening sequence

  • The HTML comment closing sequence --> is not recognized by JavaScript so it should be written as //-->.

Example

   

JavaScript variable

Variables are referred as named containers for storing information. We can place data into these containers and then refer to the data simply by naming the container.

Rules to declare variable in JavaScript

Here are the important rules that must be followed while declaring a variable in JavaScript.

  • In JavaScript variable names are case sensitive i.e. a is different from A.

  • Variable name can only be started with a underscore [ _ ] or a letter [from a to z or A to Z], or dollar [ $ ] sign.

  • Numbers [0 to 9] can only be used after a letter.

  • No other special character is allowed in variable name.

Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows −

   

Variables can be initialized at time of declaration or after declaration as follows −

   

Javascript Data Type

There are two kinds of data types as mentioned below −

  • Primitive Data Type

  • Non Primitive Data Type

The following table describes Primitive Data Types available in javaScript

Sr.No.Datatype Description
1. String

Can contain groups of character as single value. It is represented in double quotes.E.g. var x= “tutorial”.

2. Numbers

Contains the numbers with or without decimal. E.g. var x=44, y=44.56;

3. Booleans

Contain only two values either true or false. E.g. var x=true, y= false.

4. Undefined

Variable with no value is called Undefined. E.g. var x;

5. Null

If we assign null to a variable, it becomes empty. E.g. var x=null;

The following table describes Non-Primitive Data Types in javaScript

Sr.No.Datatype Description
1. Array
Can contain groups of values of same type. E.g. var x={1,2,3,55};
2. Objects
Objects are stored in property and value pair. E.g. var rectangle = { length: 5, breadth: 3};

JavaScript Functions

Function is a group of reusable statements [Code] that can be called any where in a program. In javascript function keyword is used to declare or define a function.

Key Points

  • To define a function use function keyword followed by functionname, followed by parentheses [].

  • In parenthesis, we define parameters or attributes.

  • The group of reusabe statements [code] is enclosed in curly braces {}. This code is executed whenever function is called.

Syntax

function functionname [p1, p2] {
   function coding…
}

JavaScript Operators

Operators are used to perform operation on one, two or more operands. Operator is represented by a symbol such as +, =, *, % etc. Following are the operators supported by javascript −

  • Arithmetic Operators

  • Comparison Operators

  • Logical [or Relational] Operators

  • Assignment Operators

  • Conditional [or ternary] Operators

  • Arithmetic Operators

Arithmatic Operators

Following table shows all the arithmetic operators supported by javascript −

OperatorDescriptionExample
+ Add two operands. 10 + 10 will give 20
- Subtract second operand from the first. 10 – 10 will give 0
* Multiply two operands. 10 * 30 will give 300
/ Divide numerator by denominator 10/10 will give 1
% It is called modulus operator and gives remainder of the division. 10 % 10 will give 0
++ Increment operator, increases integer value by one 10 ++ will give 11
-- Decrement operator, decreases integer value by one 10 – will give 9

Comparison Operators

Following table shows all the comparison operators supported by javascript −

OperatorDescriptionExample
== Checks if values of two operands are equal or not, If yes then condition becomes true. 10 == 10 will give true
!= Not Equal to operator
Checks if the value of two operands is equal or not, if values are not equal then condition becomes true.
10 !=10 will give false
> Greater Than operator
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
20 > 10 will give true
< Less than operator
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
10 < 20 will give true
>= Greater than or equal to operator
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
10 >=20 will give false

Chủ Đề