Chapter 1
Chapter 1
1) Giving the user more control over the browser: Example: Here you can
change the background color of this page as well as the text on the browser's
status bar.
2) Client – Side Technology: JavaScript is a client-side technology. It is mainly
used for giving client-side validation. It is an object-based scripting language.
3) Performing simple computations on the client side: Using a JavaScript
calculator, we perform simple calculations on the client side. Example. This is
a JavaScript calculator: type an arithmetic expression, and JavaScript will
compute its value.
4) Validating The User’s Input: In the JavaScript calculator, try to type some
letters instead of numeric input, you will get an error: Invalid input character.
Note that, JavaScript helps the browser perform output validation without
wasting the user’s time by the web server access.
console.log(myObj);
Variable means anything that can vary. JavaScript includes variables which hold the
data value and it can be changed anytime.
JavaScript uses reserved keyword var to declare a variable. A variable must have a
unique name. You can assign a value to a variable using equal to (=) operator when
you declare it or before using it.
Syntax:
var <variable-name>;
1) Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation
/ Division
% Modulus (Remainder)
++ Increment
-- Decrement
2) Comparison Operators
Assume variable A holds 10 and variable B holds 20, then −
Sr.No. Operator & Description
1 = = (Equal)
Checks if the value of two operands are equal or not, if yes, then the
condition becomes true.
Ex: (A == B) is not true.
2 != (Not Equal)
Checks if the value of two operands are equal or not, if the values are not
equal, then the condition becomes true.
Ex: (A != B) is true.
3 > (Greater than)
Checks if the value of the left operand is greater than the value of the right
operand, if yes, then the condition becomes true.
Ex: (A > B) is not true.
4 < (Less than)
Checks if the value of the left operand is less than the value of the right
operand, if yes, then the condition becomes true.
Ex: (A < B) is true.
5 >= (Greater than or Equal to)
Checks if the value of the left operand is greater than or equal to the value
of the right operand, if yes, then the condition becomes true.
Ex: (A >= B) is not true.
6 <= (Less than or Equal to)
Checks if the value of the left operand is less than or equal to the value of
the right operand, if yes, then the condition becomes true.
Ex: (A <= B) is true.
4) Assignment Operators
JavaScript supports the following assignment operators −
Sr.No. Operator & Description
1 = (Simple Assignment )
Assigns values from the right side operand to the left side operand
Ex: C = A + B will assign the value of A + B into C
2 += (Add and Assignment)
It adds the right operand to the left operand and assigns the result to the
left operand.
Ex: C += A is equivalent to C = C + A
3 −= (Subtract and Assignment)
It subtracts the right operand from the left operand and assigns the result
to the left operand.
Ex: C -= A is equivalent to C = C - A
4 *= (Multiply and Assignment)
It multiplies the right operand with the left operand and assigns the result
to the left operand.
Ex: C *= A is equivalent to C = C * A
5 /= (Divide and Assignment)
It divides the left operand with the right operand and assigns the result to
the left operand.
Ex: C /= A is equivalent to C = C / A
6 %= (Modules and Assignment)
It takes modulus using two operands and assigns the result to the left
operand.
Ex: C %= A is equivalent to C = C % A
Ans:
JavaScript functions are defined with the function keyword. You can use a function
declaration or a function expression.
Ans:
objectName.property // person.age
or
objectName["property"] // person["age"]
or
if Statement
if statement is used to specify a block of JavaScript code to be executed if a condition
is true.
Syntax
if (condition)
{
// block of code to be executed if the condition is true
}
Example
Make a "Good day" greeting if the hour is less than 18:00:
if (hour < 18)
{
greeting = "Good day";
}
else Statement
else statement is used to specify a block of code to be executed if the condition is
false.
Syntax
if (condition)
{
// block of code to be executed if the condition is true
} else
{
// block of code to be executed if the condition is false
}
Example
If the hour is less than 18, create a "Good day" greeting, otherwise "Good
evening":
if (hour < 18)
{
greeting = "Good day";
}
else
{
greeting = "Good evening";
}
else if Statement
else if statement is used to specify a new condition if the first condition is false.
Syntax
if (condition1)
{
// block of code to be executed if condition1 is true
}
else if (condition2)
{
// block of code to be executed if the condition1 is false and condition2 is
true
}
else
{
// block of code to be executed if the condition1 is false and condition2 is
false
}
Example
If time is less than 10:00, create a "Good morning" greeting, if not, but time is
less than 20:00, create a "Good day" greeting, otherwise a "Good evening":
if (time < 10)
{
greeting = "Good morning";
}
else if (time < 20)
{
greeting = "Good day";
}
else
{
greeting = "Good evening";
}
Switch Statement
The switch statement is used to perform different actions based on different
conditions.
Use the switch statement to select one of many code blocks to be executed.
Syntax
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Example
The getDay() method returns the weekday as a number between 0 and 6.
(Sunday=0, Monday=1, Tuesday=2 ..)
This example uses the weekday number to calculate the weekday name:
switch (new Date().getDay())
{
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
The result of day will be:
Monday
for loop
The 'for' loop includes the following three important parts −
The loop initialization where we initialize our counter to a starting value. The
initialization statement is executed before the loop begins.
The test statement which will test if a given condition is true or not. If the
condition is true, then the code given inside the loop will be executed,
otherwise the control will come out of the loop.
The iteration statement where you can increase or decrease your counter.
You can put all the three parts in a single line separated by semicolons.
Flow Chart
The flow chart of a for loop in JavaScript would be as follows −
Syntax
for (initialization; test condition; iteration statement)
{
Statement(s) to be executed if test condition is true
}
Example
<html>
<body>
<script type = "text/javascript">
<!--
var count;
document.write("Starting Loop" + "<br />");
while Loop
The most basic loop in JavaScript is the while loop which would be discussed in this
chapter. The purpose of a while loop is to execute a statement or code block
repeatedly as long as an expression is true. Once the expression becomes false, the
loop terminates.
Flow Chart
The flow chart of while loop looks as follows −
Syntax
while (expression)
{
Statement(s) to be executed if expression is true
}
Example
<html>
<body>
<script type = "text/javascript">
<!--
var count = 0;
document.write("Starting Loop ");
while (count < 10) {
document.write("Current Count : " + count + "<br />");
count++;
}
document.write("Loop stopped!");
//-->
</script>
</body>
</html>
Output
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Loop stopped!
for...in loop
Syntax
for (variablename in object)
{
statement or block to execute
}
Example
<script type = "text/javaScript">
// JavaScript program to illustrate for..in loop
// creating an Object
var languages = { first : "C", second : "Java",
third : "Python", fourth : "PHP",
fifth : "JavaScript" };
< /script>
Output:
C
Java
Python
PHP
do while loop
do while loop is similar to while loop with only difference that it checks for condition
after executing the statements, and therefore is an example of Exit Control Loop.
Syntax:
do
{
statements..
}
while (condition);
Flowchart:
Example:
<script type = "text/javaScript">
// JavaScript program to illustrate do-while loop
var x = 21;
do
{
// The line while be printer even
// if the condition is false
document.write("Value of x:" + x + "<br />");
x++;
} while (x < 20);
< /script>
Output:
Value of x: 21
Continue Statement
The continue statement terminates execution of the statements in the current iteration
of the current or labeled loop, and continues execution of the loop with the next
iteration.
In this example we use a for loop together with the continue statement.
console.log(obj.getEx());
// "this is the value of foo"
obj.setEx('hello');
console.log(obj.getEx());
// "hello"