Professional Documents
Culture Documents
Statements
Statements
• Syntax –
if (condition)
{
// block of code
}
if Statement
var x = 10;
if (x == 10)
{
document.write(“x is equal to 10”);
}
else Statement
}
switch Statement
}
Accepting User Input
• User can enter text in textbox and click on Ok button, which can
then be stored in a variable.
Accepting User Input
• E.g.,
var name = prompt(“What is your name?”);
// after this, user enters name
document.write(“Hello ” + name);
// message printed along with accepted name
Accepting User Input
• E.g.,
var num = parseInt(prompt(“Enter a Number”));
// entered number is converted to integer
var ans = num * 2;
document.write(“Answer is ” + ans);
Loop Statements
• Loop statements are used to execute a block of code a number of times.
• Loop statements in JavaScript are –
1. while
2. do-while
3. for
4. for-in
5. for-of
While Loop
• The while loop statement loops through a block of code as long as a specified
condition is true.
• Syntax –
while (condition)
{
//code of block
}
While Loop
• Example –
var x = 1;
while (x < 10)
{ document.write(x);
x = x + 1;
}
It displays numbers from 1 to 9.
Do While Loop
• Syntax –
do { //code of block
} while (condition)
Do While Loop
• Example –
var x = 10;
do
{
document.write(x);
} while (x < 10)
It displays number 10
For Loop
• Syntax -
for (expression1; expression2; expression3)
{
// block of code
}
For Loop
• Example –
for (var x = 1; x <= 5; x++)
{
document.write(x);
document.write(“<br>”);
}
It displays numbers from 1 to 5 on separate line.
For Loop
• Syntax –
for (key in object) { //code of block }
for (variable in array) { //code of block }
For of Loop
• For of loop statement loops through the values of an iterable object.
• Syntax –
for (variable of iterable) { //code of block }
Functions
• Syntax –
function name(para1, para2,..)
{
//code
}
Function Invocation
function sum(a, b)
{
return a + b;
}
var ans = sum(10,15);
document.write(“Answer is : ” + ans);
Variable Scope
• Syntax –
var array_name = [value1, value2, …];
Arrays
• Example –
var colours = [“yellow”, “blue”, “green”];
OR
var colours = [
“yellow”,
“red”,
“blue”
];
Arrays
• E.g.,
var colours = new Array(“yellow”, “green”,
“blue”);
Arrays
• E.g.,
var num = [20]; //creates array with one element
var num = new Array(20);
//creates an array with 20 undefined elements
Recognizing an Array
• E.g.,
var colours = [“yellow”, “blue”, “green”];
var length = colours.length;
document.write(length); // will print 3
Accessing Last Element
• Example –
var colours = [“yellow”, “blue”, “green”, “red”];
var colour = colours[colours.length – 1];
document.write(colour); // will print red
Looping Array Elements
• For loop can be used to loop through an array.
• Example –
var colours = [“yellow”, “blue”, “green”, “red”];
var len = colours.length;
for (var i=0; i < len; i++)
{ document.write(colours[i]);
}
Looping Array Elements
• Example –
var colours = [“yellow”, “blue”, “green”, “red”];
colours.forEach(arrFunction);
function arrFunction(element)
{ document.write(element + “<br>”);
}
Array Methods
push() join()
pop() splice()
shift() slice()
unshift() sort()
concat() reverse()
toString()
Push()
• Example –
var colours = [“yellow”, “blue”];
colours.push(“green”);
document.write(colours);
// will display three elements
Push()
• The push() method returns the length of new array.
• Example –
var colours = [“yellow”, “blue”];
var length = colours.push(“green”);
document.write(length); // will display 3
document.write(colours);
// will display three elements
Pop()
• The pop() method is used to remove the last element from an
array.
• Example –
var colours = [“yellow”, “blue”, “green”];
document.write(colours); // display three elements
colours.pop();
document.write(colours); // display yellow, blue
Pop()
• The pop() method returns the value that was popped out,
i.e., removed from an array.
• Example –
var colours = [“yellow”, “blue”, “green”];
var colour = colours.pop();
document.write(colour); // display green
shift()
• The shift() method is used to remove the first element
from an array and shift other elements to lower index.
• Example –
var colours = [“yellow”, “blue”, “green”];
colours.shift();
document.write(colours); // display blue, green
shift()
• The shift() method is used to return the value that has
been removed, i.e., the first element from an array.
• Example –
var colours = [“yellow”, “blue”, “green”];
var colour = colours.shift();
document.write(colour); // display yellow
unshift()
• The unshift() method is used to add the new element or
elements to an array at the beginning and shift older elements.
• Example –
var colours = [“yellow”, “blue”, “green”];
colours.unshift(“red”);
document.write(colours);
// display red, yellow, blue, green
unshift()
• The unshift() method returns the length of new array.
• Example –
var colours = [“yellow”, “blue”];
var length = colours.unshift(“green”);
document.write(length); // will display 3
document.write(colours);
// will display three elements
concat()
• The concat() method is used to create a new array by merging
existing arrays.
• Example –
var col1 = [“yellow”, “blue”, “green”];
var col2 = [“orange”, “black”];
colours = col1.concat(col2);
document.write(colours);
// display yellow, blue, green, orange, black
concat()
• The concat() method can take any number of array arguments.
• Example – var col1 = [“yellow”, “blue”, “green”];
var col2 = [“orange”, “black”];
var col3 = [“white”, “brown”];
colours = col1.concat(col2, col3);
document.write(colours);
// display yellow, blue, green, orange, black, white, brown
toString()
• Example –
var colours = [“yellow”, “blue”, “green”];
document.write(colours.toString());
// display yellow, blue, green
join()
• The join() method is used to join all array elements into a
string. It is similar to toString() method, but in addition, we
can specify the separator.
• Example –
var colours = [“yellow”, “blue”, “green”];
document.write(colours.join(“*”));
// display yellow * blue * green
splice()
• The splice() method is used to add new element/s to an
array at specified position and return new array .
• Syntax –
array.splice(position, count, newElement1,…)
- position defines the position from where new
elements to be added
- count defines the number of elements to be removed
splice()
• Example –
var colours = [“yellow”, “blue”, “green”];
document.write(“Original array: ” + colours);
colours.splice(2, 0, “brown”, “red”);
document.write((“Modified array: ” + colours);
// Output is –
yellow, blue, brown, red, green
splice()
• The splice() method is also used to remove elements of an
array without leaving gap in the array.
• Example –
var colours = [“yellow”, “blue”, “green”, “red”];
colours.splice(1, 1);
document.write((“Modified array: ” + colours);
// Output is – Modified array: yellow, green, red
slice()
• The slice() method is used to slice out a piece of an array into a
new array. It does not remove any element from the source array.
• Syntax –
array.slice(start, end)
- start and end defines the position in the source array to create
new array, from where to start and where to end respectively
slice()
• Example – with only start value
var colours = [“yellow”, “blue”, “green”];
document.write((“Source array: ” + colours);
var newColour = colours.slice(1);
document.write((“New array: ” + newColour);
// Output is – New array: blue, green
slice()
• Example – with start and end value
var colours = [“yellow”, “blue”, “green”, “red”];
document.write((“Source array: ” + colours);
var newColour = colours.slice(1,3);
document.write((“New array: ” + newColour);
// Output is – New array: blue, green
sort()
• Syntax –
array.sort()
sort()
• Example –
var colours = [“yellow”, “blue”, “green”, “red”];
document.write((“Source array: ” + colours);
colours.sort();
document.write((“Sorted array: ” + colours);
// Sorted array: blue, green, red, yellow
reverse()
• Syntax –
array.reverse()
reverse()
• Example –
var colours = [“yellow”, “blue”, “green”, “red”];
document.write((“Source array: ” + colours);
colours.sort();
colours.reverse();
document.write((“Sorted reverse array: ” + colours);
// Sorted reverse array: yellow, red, green, blue
Multi-dimensional Array
• In multi-dimensional array, one array is an element of
another array.