JS Notes
JS Notes
A JavaScript variable is simply a name of storage location. There are two types of variables in JavaScript : local
variable and global variable.
There are some rules while declaring a JavaScript variable (also known as identifiers).
1.Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
2.After first letter we can use digits (0 to 9), for example value1.
3.JavaScript variables are case sensitive, for example x and X are different variables.
<script>
var x=50; //gloabal variable
function fun1()
{
document.writeln(x);
}
function fun2()
{
document.writeln(x);
}
fun1();//calling JavaScript function
fun2();
</script>
A JavaScript global variable is accessible from any function. A variable i.e. declared outside the function or
declared with window object is known as global variable.
Declaring JavaScript global variable within function
To declare JavaScript global variables inside function, you need to use window object.
function m(){
window.value=100;//declaring global variable by window object
}
function n(){
alert(window.value);//accessing global variable from other function
}
Javascript Data Types
JavaScript provides different data types to hold different types of values. There are two types of data
types in JavaScript.
1.Primitive data type
2.Non-primitive (reference) data type
JavaScript primitive data types JavaScript non-primitive data types
Data Type Description Data Type Description
represents sequence of characters e.g.
String represents instance through which
"hello" Object
we can access members
Number represents numeric values e.g. 100
represents boolean value either false or
Boolean Array represents group of similar values
true
Undefined represents undefined value
RegExp represents regular expression
Null represents null i.e. no value at all
JavaScript Operators
There are following types of operators in JavaScript.
1.Arithmetic Operators
2.Comparison (Relational) Operators
3.Bitwise Operators
4.Logical Operators
5.Assignment Operators
6.Special Operators
Syntax
if(expression)
{
//content to be evaluated
}
Example
<script language=“javascript”>
var a=20;
if(a>10)
{
document.write("value of a is greater than 10");
}
</script>
JavaScript If...else Statement
Syntax Example
if(expression) <script language=“javascript”>
{ var a=20;
//content to be evaluated if condition is true if(a%2==0)
} {
else document.write("a is even number");
{ }
//content to be evaluated if condition is false else
} {
document.write("a is odd number");
}
</script>
JavaScript If...else if Statement
Syntax Example
if(expression1) <script>
{ var a=20;
//content to be evaluated if expression1 is true if(a==10)
} {
document.write("a is equal to 10");
else if(expression2)
}
{
else if(a==15)
//content to be evaluated if expression2 is true
{
} document.write("a is equal to 15");
else if(expression3) }
{ else if(a==20)
//content to be evaluated if expression3 is true {
} document.write("a is equal to 20");
else }
{ else
//content to be evaluated if no expression is true {
} document.write("a is not equal to 10, 15 or 20");
}
</script>
JavaScript switch Statement
Example
Syntax <script>
switch(expression) var grade='B';
{ var result;
switch(grade)
case value1:
{
code to be executed;
case 'A':
break;
result="A Grade";
case value2: break;
code to be executed; case 'B':
break; result="B Grade";
...... break;
case 'C':
default: result="C Grade";
code to be executed; break;
} default:
result="No Grade";
}
document.write(result);
</script>
JavaScript for loop
Syntax Example
for (initialization; condition; increment) <script>
{ for (i=1; i<=5; i++)
code to be executed {
} document.write(i + "<br/>")
}
</script>
JavaScript while loop
Syntax Example
while (condition) <script>
{ var i=11;
code to be executed while (i<=15)
} {
document.write(i + "<br/>");
i++;
}
</script>
JavaScript do-while loop
Syntax Example
do <script>
{ var i=21;
code to be executed do
}while (condition); {
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>
Explanation
Syntax for-in
for (var in expression) A for...of loop operates on the values sourced from an iterable one by one in
{ sequential order. Each operation of the loop on a value is called an iteration,
//statement(s) and the loop is said to iterate over the iterable. Each iteration executes
} statements that may refer to the current sequence value.
When a for...of loop iterates over an iterable, it first calls the iterable's
Syntax for-of [@@iterator]() method, which returns an iterator, and then repeatedly calls the
resulting iterator's next() method to produce the sequence of values to be
for (variable of iterable)
assigned to variable.
{
A for...of loop exits when the iterator has completed (the iterator's next()
statement
method returns an object containing done: true). You may also use control
}
flow statements to change the normal control flow. break exits the loop and
Variable -Receives a value from the goes to the first statement after the loop body, while continue skips the rest of
sequence on each iteration. the statements of the current iteration and proceeds to the next iteration.
Iterable -An iterable object. The source of If the for...of loop exited early (e.g. a break statement is encountered or an
the sequence of values on which the loop error is thrown), the return() method of the iterator is called to perform any
operates cleanup.
The variable part of for...of accepts anything that can come before the =
operator. You can use const to declare the variable as long as it's not reassigned
within the loop body (it can change between iterations, because those are two
separate variables).
// Example-1 :- array // Example-2 :- string //Example-3 :-
const students = ['John', 'Sara', 'Jack’]; const string = 'code’; var list1 = [10,20,30,40,50];
// using for...of // using for...of loop
for ( let element of students ) for (let i of string) for(var i in list1)
{ { {
// display the values document.write(i); document.write(i);
document.write(element); } }
}
Output
Output c
John o
Sara d
Jack e