clAssmate
Data
Page 7
1. To make Code easy to understand.
2 To avoid the unnecessary code.
Types of Javascript comments.
1. Single - line comment
2 Multi- line Comment
Java Script Single line Comment
It is represented by double forward slashes (//)
It Can be used before and after the Stalement.
<script >
//It is single line comment
document. Write ("hello javascript");
</script>
Java Script Multi line Comment
It Can be used to add single as well as multi
line comments. So, it is more convenient.
your code here */
JavaScript Varjable
A Javascript Variable is simply a name of storage
location. There are two types of variables in JS:
local variable and global Variable.
Rules for declaring Variable
1. Name must start with a letter Ca tuz or A toZ),
underscore (-), or dollar ($.) sign.
2 After first letter we can use digits (oto9). for example
Nalue 1.
3. Javascript Variables are case sensitive.
Dote
Poge 8
t. l o c a l v a riable
Java Sc re jp
l e is d e c l a r e d inside block
local varia b
A Javascript wi th in th e function or
ac ce ss ib le
Or function. It is
block ohly
<Script>
function abc ()}
Var x = 10://local variab
le
3
</Script>
JavaScript global Varjable
A Java Script global Variable is acc
essible from any
the function
function. A Variable i.e. declared outside
or declared With window object is kno
w as global
Variable.
<Script >
Var data = 200; 1/ global Variable
function a()E
导
</script>
JavaScript Data Types
Javascript provides different data types to hold
different types of values. There are two types
of data types in Java Script.
1. Primitive data type
2. Non - primitive data type
Var a = 40;//holding number
Var b = "Rahul"; // holding String
classmate
Pata
و
Java Script Primitive data types
Data Type Description
String represents sequence of characterse.g."hello"
Number Pepresents numeric Values e.9. 100
Boolean Pepresents boolean Value cither false ortrue
Underfined represents undefined Value
Null represents null i.e. no Value at all
•JavaScript non-primitive data types
Data Type Description
Object represents instance through Which We Can access members
Arpay represents group of Similar values
RegExp |represents regular ex pression
JavaScript Operators
JavaScript Operators areSymbols that are used
to perform Operations on operands.
Javascript. Arithmetic Operators
Arithmetic operators are used to perform arithmetic
operations on the operands.
Operator Description Example
+ Addition 10 +20 =30
Subtraction 20--10. = 10
Multiplication 10*20 = 200
Division 20/10= 2
% Modulus 20%10 = О.
++ Increment Var a = 10;a++; Now a = 11
11
Decrement Var a = 10; Q--; Nowa = 9
ClASSmaLe
Dofe
Pege 10
JavaScript Comparison Operators
6
The JavaScript Comparison operator Compares the 2 operandS
Operator Description Example
10==20 = false
Is equal to
Indentical 10==20 = false
=
Not equal to 10!=20 = true
!==
Not Identical 20!==20 = false
> Greater than 20 >10 = trua
>= Greater than or equal to 20>=10 = true
く Less than 20 <10= false
く=
Less than or equal to '20<=10 = false
JavaScript Bitwise Operators
The bit wise operators perform betwise operations on operands
Operator Description Exam ple
& Bitwise AND (10==20$ 20==33)=false
Bitwise OR (10==20|20==33)=false
Λ Bitwise XOR (10==20120==33)=false
2
BitWise NOT (~10)= -10
くく Bitwise Left Shift (10<<2)=40
>> Bitwise Right Shift (10>>2) = 2
>>> Bitwise Right Shift 0 (107772)=2
Java Script Logical Operators
The following operators are khown as JavaScript logical operators.
Operator Descript ion Example
&& Logical AND (10==204&20==33) = false
i Logical OR (10==20||20==33)=false
Logical Not 1(10==20) = truе
clAssmate
Date
Page
Java Script Assignment Operators
The fllowing Operators are known as Javascript assigniment operators.
Operator Description Example
=
Assigh 10+10 =20
+= Add and assigh Vara=10; Q+ =20; NOW a=30
Subtract and assign Vara=20; 0-=10; Nowa=10
-=
*ー
Multiply and assign Vara=10; a= 20; Now a=200
/= Divide and assigh Vara=10; Q/=2;NOw a = 5
%=
Modulus and assigh Var a = 1o; a % =2; Now a = 0
Java Script. Special Operators
The following Operators are knownas Javascript special operators.
Operator Description
(?:) Conditional Operator returns Value based on
the Condition. It is like if- else.
S
Comma Operator allows multiple expressions
to be evaluated as single statement.
delete Delete Operator deletes a property from
the object.
jn In Operator checks if object has the given
property.
instanceof Checks if the object is an instance of given
type
new Creates an instance
typeof Checks the type of object
Void it discards the expression's return value.
yield Checks What is returned in a generator
by the generator's interator.
ClAsEm
ate
Dote
Page 12
Java Script If- else
The Java Script if-else Statement is used to
execute the code Whether condition is true Or
False. There are three forms of if statement in
JavaScript.
4. If Statement
2. If else statement
3. 1f else if statement
Javascript. If Statement
It evaluates the Content only if expression is true.
False
Condition
True
if code
After if
Example
<script >
Var a = 20;
if (a>10)
document. Write ("Value of a is grater than 10");
}
</script>
clAssmate
Date
Page 13
JavaScript if. else statement
If evaluates the content Whether condition is true of false-
Condition Fatse
True
A
If code else code
After if
Examde
<Script 7
Var a = 20;
if (a%2 = 0){
document. Write ("a is even number")
3
else
document. Write ("a is odd number");
3
</Script >
JavaScript If... else if statement
It evaluates the content Only if expression is true.
Example
<script>
Var a = 20;
|f (a= 1o){
=
document. Write ("a is equal to 10");
ClasSma
te
Date
Poge 4
3
else if (a = = 15) {
[Link] ("a is equal to 15");
}
else if (a= = 20){
[Link] ("a is equal to 20");
3
else&
document. Write ('a is not egual to 10, 15 0r 20");
3
</script>
Javascript Switch t
The Javascript Switch statement is used to execute
One code from multiple expressions.
Example
<Script>
Var grade = 'B';
Var result;
Switch (grade){
Case 'A;
Pesult ="A Grade";
breaki
Case 'B:
Pesult ="B Grade";
break
Case'C'
result ="C Grade";
break;
default;
clesemate
Fige 15
result = "No Grade";
3
document. Write (result);
</script >
Output
B Grade
JavaScript Loops
The JavaScript loops used to iterate the piece of code
are
using for, While, do while or for-in loops. It makes the
Code compact. It is mostly used in array.
There are four types of loops in JavaScript.
1) Javascript For loop
The Javascript for loop iterates the elements for the fixed
number of times. It should be used if number of inte-
ration is known.
Example Output
< script> 1
for (j= 1;i<
3 = 5; j++) 2
3
[Link] (it "<br />") 4
} 5
</ script>
2) JavaScript While loop
The Javascript While loop iterates the elements for the
infinite number of times. It Should be used if number
of iteration is not known.
CLaSS
mate
Date
Page 6
Example Output
11
<Script>
Var i = 11;
12
While (i<=15)
B 13
14
document. Write (it"<bp/>"); 15
++;
3
</script>
3) Java Script do While loop
The Javascript do While loop iterates the elements
for the infinite number of times like while loop.
But, code is executed at least Once Whether Condition
is true or false.
Example
Output
<Script> 21
Var i = 21;
22
do {
23
document. Write (i+ "< br /> "); 24
j++;
25
3 While (i<=25);
</Script >
JavaScript Functions
JavaScript functions are used to perform operations.
We fan Call Javascript function many times to reuse.
the code.
CHASSMALe
Dote
Fege 17
Advantage of JavaScript function
1. Code reusability
2. Less coding
JavaScript Function Example
<
Script>
function msg.(){
alert ("hello! this is message"):
3
</script>
<input type = "button" onclick ='msg()"value="Call function"/
Output call function
JavaScript Function Arguments
We Can Call function by passing arguments.
Example
<script >
function getcube (number)3
alert (number* number*number);
</script >
<form>
<input type = "button"= "Click" onclick = "getcube(4)"/>
</form>
Output Click
JavaScript Function Object
In JavaScript, the purpose of Function Constructor is