Introduction to JavaScript
Introduction
- JavaScript is a lightweight language
- It is interpreted programming language
- It is designed for creating network-centric applications
- It is complimentary and integrated with Java
- It is very easy to implement because it is integrated with HTML
- It is open and crossed platform
Features of JavaScript
- It is most popular scripting and programming language
- JavaScript is everywhere, it comes on every modern web-browser
- It does not need any special environment setup
- It helps to create beautiful and interactive websites
- It has lots of frameworks and libraries
Advantages of JavaScript
- Less Server interaction
- Validate user inputs at Client Side, no load on server
- Immediate feedback to the visitors
- Increased connectivity
- Create interfaces to react when user hovers mouse over components
- Richer interface
- Includes items as drag & drop components and sliders to give a Rich Interface to websites
Program in JavaScript
Data Types
Number
Infinity
NaN (Not a Number)
String
- Double Quotes : "Welcome"
- Single Quotes : 'Welcome'
- Back Ticks : ` Welcome `
Boolean
Branching
if Statement
Is a
fundamental control statement
Allows
JavaScript to make decision and execute statement conditionally
Syntax
if(expression)
{
Statement to be executed if
expression is true
}
if … else Statement
Is the next form of the control
statement
Allows JavaScript to execute
statements in more controlled way
Syntax
if(expression)
{
Statement
to be executed if expression is true
}
else
{
Statement
to be executed if expression is false
}
if … else if … Statement
Is an advanced form of if … else
Allows JavaScript to make a correct
decision out of several conditions
It allows JavaScript to execute the
multiple statement as per the true expression
Syntax
if(expression)
{
Statement
to be executed if expression is true
}
else if (expression1)
{
Statement
to be executed if expression1 is true
}
else if (expression2)
{
Statement
to be executed if expression2 is true
}
else
{
Statement to be
executed if all above expressions are false
}
switch Case
The switch case is used to evaluate
an expression and execute several statements based on the value of expression
The interpreter verifies the case
against the value of the expression
If match is found with the case,
then statement of the case executed
If nothing matches, then a default
condition will be used.
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 end a
particular case,
If break is not there it will
continue to execute further case
The while Loop
The most basic loop used in
JavaScript
The purpose of the while loop is to
execute the statements or code block repeatedly as long as expression true.
Once the expression is false, the
loop terminates
Syntax:
while(expression)
{
statement to be
executed if the expression is true
}
The do … while Loop
The task of the while and the do …
while is same, except that the expression check happens at the end of the loop.
The loop will always be executed at
least once, even if the expression is false.
Syntax:
do
{
Statement(s)
to be executed;
} while
(expression) ;
Semicolon is compulsory after while,
so that the loop will get terminate if expression is false.
for Loop
It is most compact form of looping,
which includes 3 important parts as follows:
•
Loop
Initialization: Initializes counter to a starting value, it executes before
loop begins.
•
Test
Statement: Test expression, if expression is true it executes the code block of
loop, if false, it terminates the loop.
•
Iteration
Statement: Increases or decreases the value of the counter.
Syntax:
for
( initialization ; test condition ; iteration statement )
{
Statement(s)
to be executed if test condition is true
Comments
Post a Comment