3 JAVASCRIPT (Unit 3)_380629ce76217ebcb335068efe6aa9ae_1
3 JAVASCRIPT (Unit 3)_380629ce76217ebcb335068efe6aa9ae_1
<html>
<body>
</body>
</html>
Parul Saxena, MITS 9
Using window.alert()
<html>
<body>
<script>
console.log(5 + 6);
</script>
</body>
</html>
• Keyword Description
• Break Terminates a switch or a loop
• Continue Jumps out of a loop and starts at the top
• do ... While Executes a block of statements, and repeats
the block, while a condition is true
• For Marks a block of statements to be executed, as
long as a condition is true
• Function Declares a function
• if ... Else Marks a block of statements to be executed,
depending on a condition
• Return Exits a function
• Switch Marks a block of statements to be executed,
depending on different cases
• try ... Catch Implements error handling to a block of
statements
• var Declares a variable
<h2>JavaScript Variables</h2>
<p id="demo"></p>
<script>
var x;
x = 6;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<html>
<body>
<h2>The ** Operator</h2>
<p id="demo"></p>
<script>
var x = 5;
document.getElementById("demo").innerHTML = x ** 2;
</script>
</body>
</html>
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>
<p id="demo"></p>
</body>
</html>
<html>
<body>
</body>
</html> //js6.html
<p>In this case JavaScript changes the value of the src (source) attribute of an image.</p>
</body>
</html>
// js7.html
Parul Saxena, MITS 35
JavaScript Can Change HTML Styles (CSS)
• Changing the style of an HTML element, is a variant of changing an HTML
attribute:
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can change the style of an HTML element.</p>
<button type="button"
onclick="document.getElementById('demo').style.fontSize='35px'">Click
Me!</button>
</body>
</html>
</body>
</html>
</body>
</html>
<h2>JavaScript in Body</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML="My First JavaScript";
</script>
</body>
</html>
<h2>JavaScript in Head</h2>
</body>
</html>
Parul Saxena, MITS 41
JavaScript in <body>
• In this example, a JavaScript function is placed in the <body> section of an
HTML page.
• The function is invoked (called) when a button is clicked:
<html>
<body>
<h2>JavaScript in Body</h2>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</body>
</html> Parul Saxena, MITS 42
External JavaScript
• Scripts can also be placed in external files:
• External file: myScript.js
• function myFunction() {
document.getElementById("demo").innerHTML = "Para
graph changed.";
}
• JavaScript files have the file extension .js.
• To use an external script, put the name of the script file in
the src (source) attribute of a <script> tag:
• Example
• <script src="myScript.js"></script>
<html>
<body>
<h2>External JavaScript</h2>
<script src="myScript.js"></script>
</body>
</html>
<html>
<body>
</body>
</html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>This example calls a function which performs a calculation, and returns the result:</p>
<p id="demo"></p>
<script>
function myFunction(p1, p2) {
return p1 * p2;
}
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>
</body>
</html>
<h2>JavaScript Objects</h2>
<p id="demo"></p>
<script>
// Create an object:
var car = {type:"Fiat", model:"500", color:"white"};
</body>
</html>
<h2>JavaScript Objects</h2>
<p id="demo"></p>
<script>
// Create an object:
var person = {firstName:"John", lastName:"Roy", age:50, eyeColor:"blue"};
</body>
</html>
<h2>JavaScript Objects</h2>
<p id="demo"></p>
<script>
// Create an object:
var person = {
firstName: "John",
lastName : “Roy",
id : 5566
};
// Display some data from the object:
document.getElementById("demo").innerHTML = person["firstName"] + " " + person["lastName"];
</script>
</body>
</html>
<button
onclick="document.getElementById('demo').innerHTML=Date()">T
he time is?</button>
<p id="demo"></p>
</body>
</html>
<h2>JavaScript Strings</h2>
<p>You can use quotes inside a string, as long as they don't match the quotes
surrounding the string.</p>
<p id="demo"></p>
<script>
var answer1 = "It's alright";
var answer2 = "He is called 'Johnny'";
var answer3 = 'He is called "Johnny"';
document.getElementById("demo").innerHTML =
answer1 + "<br>" + answer2 + "<br>" + answer3;
</script>
</body>
</html>
<p id="demo"></p>
<script>
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.getElementById("demo").innerHTML = txt.length;
</script>
</body>
</html>
<h2>JavaScript Strings</h2>
<p id="demo"></p>
<script>
var x = "We are the \"BEST\".";
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<h2>JavaScript Strings</h2>
<p id="demo"></p>
<script>
var x = 'It\'s alright.';
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<h2>JavaScript Strings</h2>
<p id="demo"></p>
<script>
var x = "The character \\ is called backslash.";
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<h2>JavaScript Strings</h2>
<p>
You can break a code line within a text string with a backslash.
</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello \
Dolly!";
</script>
</body>
</html>
<p id="demo"></p>
<script>
var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate");
document.getElementById("demo").innerHTML = pos;
</script>
</body>
</html>
<p id="demo"></p>
<script>
var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate");
document.getElementById("demo").innerHTML = pos;
</script>
</body>
</html>
<p id="demo"></p>
<script>
var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate",15);
document.getElementById("demo").innerHTML = pos;
</script>
</body>
</html>
<p>The lastIndexOf() method accepts a second parameter as the starting position for the search.</p>
<p>Remember that the lastIndexOf() method searches backwards, so position 15 means start the
search at position 15, and search to the beginning.</p>
<p>Position 15 is position 15 from the beginning.</p>
<p id="demo"></p>
<script>
var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate", 15);
document.getElementById("demo").innerHTML = pos;
</script>
</body>
</html>
<p id="demo"></p>
<script>
var str = "Apple, Banana, Kiwi";
var res = str.slice(7,13);
document.getElementById("demo").innerHTML = res;
</script>
</body>
</html>
<p id="demo"></p>
<script>
var str = "Apple, Banana, Kiwi";
var res = str.slice(-12,-6);
document.getElementById("demo").innerHTML = res;
</script>
</body>
</html>
<p id="demo"></p>
<script>
var str = "Apple, Banana, Kiwi";
var res = str.slice(7);
document.getElementById("demo").innerHTML = res;
</script>
</body>
</html>
<p id="demo"></p>
<script>
var str = "Apple, Banana, Kiwi";
var res = str.slice(-12)
document.getElementById("demo").innerHTML = res;
</script>
</body>
</html>
<p id="demo"></p>
<script>
var str = "Apple, Banana, Kiwi";
var res = str.substr(-4);
document.getElementById("demo").innerHTML = res;
</script>
</body>
</html>
<body>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txt = str.replace("Microsoft","Yahoo");
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txt = str.replace("Microsoft","Yahoo");
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
Parul Saxena, MITS 92
• By default, the replace() method is case
sensitive. Writing MICROSOFT (with upper-
case) will not work:
• Example
• str = "Please visit Microsoft!";
var n = str.replace("MICROSOFT", “Yahoo!");
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txt = str.replace("MICROSOFT","Yahoo!");
document.getElementById("demo").innerHTML = txt;
}
</script>
<p><strong>Note:</strong> Nothing will happen. By default, the replace() method is case sensitive.
Writing MICROSOFT (with upper-case) will not work.</p>
</body>
</html>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txt = str.replace(/MICROSOFT/i,"Yahoo!");
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txt = str.replace(/Microsoft/g,"Yahoo");
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
<script>
function myFunction() {
var text = document.getElementById("demo").innerHTML;
document.getElementById("demo").innerHTML = text.toUpperCase();
}
</script>
</body>
</html>
<p id="demo"></p>
<script>
var text1 = "Hello";
var text2 = "World!";
var text3 = text1.concat(" ",text2);
document.getElementById("demo").innerHTML = text3;
</script>
</body>
</html>
<h2>JavaScript String.trim()</h2>
<p><strong>Note:</strong> The trim() method is not supported in Internet Explorer 8 and earlier
versions.</p>
<script>
function myFunction() {
var str = " Hello World! ";
alert(str);
}
</script>
</body>
</html>
<p id="demo"></p>
<script>
var str = "HELLO WORLD";
document.getElementById("demo").innerHTML = str.charAt(0);
</script>
</body>
</html>
<p>Click "Try it" to display the first array element, after a string split.</p>
<p id="demo"></p>
<script>
function myFunction() {
var str = "a,b,c,d,e,f";
var arr = str.split(",");
document.getElementById("demo").innerHTML = arr[0];
}
</script>
</body>
</html>
• var x = 10;
var y = 20;
var z = x + y; // z will be 30 (a number)
• var x = "10";
var y = "20";
var z = x + y; // z will be 1020 (a string)
• var x = 10;
var y = "20";
var z = x + y; // z will be 1020 (a string)
• var x = "10";
var y = 20;
var z = x + y; // z will be 1020 (a string)
• var x = 10;
var y = 20;
var z = "The result is: " + x + y; //?????
• The result is: 1020
• var x = "100";
var y = "10";
var z = x * y;
• // z will be 1000
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Number(true) + "<br>" +
Number(false) + "<br>" +
Number("10") + "<br>" +
Number(" 10") + "<br>" +
Number("10 ") + "<br>" +
Number(" 10 ") + "<br>" +
Number("10.33") + "<br>" +
Number("10,33") + "<br>" +
Number("10 33") + "<br>" +
Number("John");
</script>
</body>
</html>
• Example
• var cars = ["Saab", "Volvo", "BMW"];
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
var cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars;
</script>
</body>
</html>
<h2>JavaScript Arrays</h2>
<p>Array indexes are zero-based, which means the first item is [0].</p>
<p id="demo"></p>
<script>
var cars = ["Saab","Volvo","BMW"];
document.getElementById("demo").innerHTML = cars[0];
</script>
</body>
</html>
<h2>JavaScript Objects</h2>
<p>JavaScript uses names to access object properties.</p>
<p id="demo"></p>
<script>
var person = {firstName:"John", lastName:"Doe", age:46};
document.getElementById("demo").innerHTML =
person["firstName"];
</script>
</body>
</html>
Parul Saxena, MITS 127
Array Properties and Methods
• The real strength of JavaScript arrays are the built-in
array properties and methods:
• Examples
• var x = cars.length; // The length property returns the
number of elements
var y = cars.sort(); // The sort() method sorts arrays
• The length Property
• The length property of an array returns the length of an
array (the number of array elements).
• Example
• var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.length; // the length of fruits is 4
<h2>JavaScript Arrays</h2>
<p>The length property returns the length of an array.</p>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.length;
</script>
</body>
</html>
text = "<ul>";
for (i = 0; i < fLen; i++) {
text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>";
Parul Saxena, MITS 131
<html>
<body>
<h2>JavaScript Arrays</h2>
<p>The best way to loop through an array is using a standard for loop:</p>
<p id="demo"></p>
<script>
var fruits, text, fLen, i;
fruits = ["Banana", "Orange", "Apple", "Mango"];
fLen = fruits.length;
text = "<ul>";
for (i = 0; i < fLen; i++) {
text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>";
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
text = "<ul>";
fruits.forEach(myFunction);
text += "</ul>";
function myFunction(value) {
text += "<li>" + value + "</li>";
}
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
var fruits, text;
fruits = ["Banana", "Orange", "Apple", "Mango"];
text = "<ul>";
fruits.forEach(myFunction);
text += "</ul>";
document.getElementById("demo").innerHTML = text;
function myFunction(value) {
text += "<li>" + value + "</li>";
}
</script>
</body>
</html>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.push("Lemon");
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
<h2>JavaScript Arrays</h2>
<p>The length property provides an easy way to append new elements to an array without using the
push() method.</p>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits[fruits.length] = "Lemon";
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
<h2>JavaScript Arrays</h2>
<p>Adding elements with high indexes can create undefined "holes" in an array.</p>
<p id="demo"></p>
<script>
var fruits, text, fLen, i;
fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[6] = "Lemon";
fLen = fruits.length;
text = "";
for (i = 0; i < fLen; i++) {
text += fruits[i] + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<h2>pop()</h2>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
fruits.pop();
document.getElementById("demo2").innerHTML = fruits;
</script>
</body>
</html>
<h2>pop()</h2>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
document.getElementById("demo2").innerHTML = fruits.pop();
document.getElementById("demo3").innerHTML = fruits;
</script>
</body>
</html>
<h2>push()</h2>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.push("Kiwi");
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
<h2>push()</h2>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
function myFunction() {
document.getElementById("demo2").innerHTML = fruits.push("Kiwi");
document.getElementById("demo1").innerHTML = fruits;
}
</script>
</body>
</html>
<h2>shift()</h2>
<p>The shift() method returns the element that was shifted out.</p>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
document.getElementById("demo2").innerHTML = fruits.shift();
document.getElementById("demo3").innerHTML = fruits;
</script>
</body>
</html>
<h2>unshift()</h2>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.unshift("Lemon");
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
<h2>unshift()</h2>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
document.getElementById("demo2").innerHTML = fruits.unshift("Lemon");
document.getElementById("demo3").innerHTML = fruits;
</script>
</body>
</html>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML =
"The first fruit is: " + fruits[0];
delete fruits[0];
document.getElementById("demo2").innerHTML =
"The first fruit is: " + fruits[0];
</script>
</body>
</html>
<h2>splice()</h2>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = "Original Array:<br>" + fruits;
function myFunction() {
fruits.splice(2, 0, "Lemon", "Kiwi");
document.getElementById("demo2").innerHTML = "New Array:<br>" + fruits;
}
</script>
</body>
</html>
<h2>splice()</h2>
<p>The splice() method adds new elements to an array, and returns an array with the deleted elements (if any).</p>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = "Original Array:<br> " + fruits;
function myFunction() {
var removed = fruits.splice(2, 2, "Lemon", "Kiwi");
document.getElementById("demo2").innerHTML = "New Array:<br>" + fruits;
document.getElementById("demo3").innerHTML = "Removed Items:<br> " + removed;
}
</script>
</body>
</html>
<h2>splice()</h2>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.splice(0, 1);
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
<h2>concat()</h2>
<p id="demo"></p>
<script>
var fruit = ["Apple", "Mango"];
var vegetable = ["Tomato", "Potato", "Onion"];
var inAll = fruit.concat(vegetable);
document.getElementById("demo").innerHTML = inAll;
</script>
</body>
</html>
<html>
<body>
<h2>concat()</h2>
<p id="demo"></p>
<script>
var arr1 = ["Apple", "Mango"];
var arr2 = ["Tomato", "Potato", "Onion"];
var arr3 = ["Jeera", "Meetha Neem"];
document.getElementById("demo").innerHTML = inAll;
</script>
</body>
</html>
<h2>concat()</h2>
<p id="demo"></p>
<script>
var arr1 = ["Apple", "Mango", "Banana"];
var inAll = arr1.concat("Grapes");
document.getElementById("demo").innerHTML = inAll;
</script>
</body>
</html>
<h2>slice()</h2>
<p>This example slices out a part of an array starting from array element
("Orange"):</p>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1);
document.getElementById("demo").innerHTML = fruits + "<br><br>" + citrus;
</script>
</body>
</html>
<p id="demo"></p>
<script>
// Create and display an array:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
// First sort the array
fruits.sort();
// Then reverse it:
fruits.reverse();
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
<p>The AND operator (&&) returns true if both expressions are true, otherwise it returns
false.</p>
<p id="demo"></p>
<script>
var x = 6;
var y = 3;
document.getElementById("demo").innerHTML =
(x < 10 && y > 1) + "<br>" +
(x < 10 && y < 1);
</script>
</body>
</html>
<h2>JavaScript Comparison</h2>
<p>The OR operator (||) returns true if one or both expressions are true, otherwise it returns false.</p>
<p id="demo"></p>
<script>
var x = 6;
var y = 3;
document.getElementById("demo").innerHTML =
(x == 5 || y == 5) + "<br>" +
(x == 6 || y == 0) + "<br>" +
(x == 0 || y == 3) + "<br>" +
(x == 6 || y == 3);
</script>
</body>
</html>
<h2>JavaScript Comparison</h2>
<p>The NOT operator (!) returns true for false statements and false for true statements.</p>
<p id="demo"></p>
<script>
var x = 6;
var y = 3;
document.getElementById("demo").innerHTML =
!(x === y) + "<br>" +
!(x > y);
</script>
</body>
</html>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
var x = 5;
document.getElementById("demo").innerHTML = (x === 5);
</script>
</body>
</html>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
var x = 5;
document.getElementById("demo").innerHTML = (x === "5");
</script>
</body>
</html>
• Example
var car;
document.getElementById("demo").innerHTML = car + "<br>" + typeof car;
• Syntax
variablename = (condition) ? value1:value2
• Example
var voteable = (age < 18) ? "Too young":"Old
enough";
<p id="demo"></p>
<script>
function myFunction() {
var age, voteable;
age = document.getElementById("age").value;
voteable = (age < 18) ? "Too young":"Old enough";
document.getElementById("demo").innerHTML = voteable + " to vote.";
}
</script>
</body>
</html>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 2 < "John";
</script>
</body>
</html>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 2 == "John";
</script>
</body>
</html>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "2" < "12";
</script>
</body>
</html>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "2" > "12";
</script>
</body>
</html>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "2" ==
"12";
</script>
</body>
</html>
Parul Saxena, MITS 190
• alert( 'Z' > 'A' ); // true
• alert( 'Glow' > 'Glee' ); // true
• alert( 'Bee' > 'Be' ); // true
<h2>JavaScript Comparisons</h2>
<p id="demo"></p>
<script>
function myFunction() {
var age, voteable;
age = Number(document.getElementById("age").value);
if (isNaN(age)) {
voteable = "Input is not a number";
} else {
voteable = (age < 18) ? "Too young" : "Old enough";
}
document.getElementById("demo").innerHTML = voteable;
}
</script>
</body>
</html>
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
</script>
<p>Set the variable to different value and then try...</p>
</body>
<html>
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
Parul Saxena, MITS 201
The While Loop
• The while loop loops through a block of code
as long as a specified condition is true.
• Syntax
• while (condition) {
// code block to be executed
}
<p id="demo"></p>
<script>
var text = "";
var i = 0;
while (i < 10) {
text += "<br>The number is " + i;
i++;
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<p id="demo"></p>
<script>
var text = ""
var i = 0;
do {
text += "<br>The number is " + i;
i++;
}
while (i < 10);
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
var count;
document.write("Starting Loop" + "<br />");
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
<p id="demo"></p>
<script>
var txt = "";
var person = {fname:"John", lname:"Doe", age:25};
var x;
for (x in person) {
txt += person[x] + " ";
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
<script>
var cars = ['BMW', 'Volvo', 'Mini'];
var x;
for (x of cars) {
document.write(x + "<br >");
}
</script>
</body>
</html>
<script>
var txt = 'JavaScript';
var x;
for (x of txt) {
document.write(x + "<br >");
}
</script>
</body>
</html>
<h2>JavaScript Loops</h2>
<p id="demo"></p>
<script>
var text = "";
var i;
for (i = 0; i < 10; i++) {
if (i === 3) { break; }
text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<h2>JavaScript Loops</h2>
<p id="demo"></p>
<script>
var text = "";
var i;
for (i = 0; i < 10; i++) {
if (i === 3) { continue; }
text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<h2>JavaScript break</h2>
<p id="demo"></p>
<script>
var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = "";
list: {
text += cars[0] + "<br>";
text += cars[1] + "<br>";
break list;
text += cars[2] + "<br>";
text += cars[3] + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
</script>
</body>
</html>
</script>
</body>
</html>
<p>The typeof operator returns the type of a variable, object, function or expression.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
typeof "john" + "<br>" +
typeof 3.14 + "<br>" +
typeof NaN + "<br>" +
typeof false + "<br>" +
typeof [1,2,3,4] + "<br>" +
typeof {name:'john', age:34} + "<br>" +
typeof function () {} + "<br>" +
typeof myCar + "<br>" +
typeof null;
</script>
</body>
</html>
<p id="demo"></p>
<script>
function myFunction() {
var y = "5";
var x = + y;
document.getElementById("demo").innerHTML = typeof y + "<br>" + typeof x;
}
</script>
</body>
</html>
<h2>JavaScript Comparison</h2>
<p id="demo"></p>
<script>
var x = 5;
document.getElementById("demo").innerHTML = (x === "5");
</script>
</body>
</html>
• Example
• var y = "John"; // y is a string
var x = + y; // x is a number (NaN)
<p id="demo"></p>
<script>
function myFunction() {
var y = "John";
var x = + y;
document.getElementById("demo").innerHTML = typeof y + "<br>" + typeof x;
}
</script>
</body>
</html>
<p id="demo"></p>
<script>
var x = [];
document.getElementById("demo").innerHTML =
(5 + null) + "<br>" +
("5" + null) + "<br>" +
("5" + 2) + "<br>" +
("5" - 2) + "<br>" +
("5" * "2") + "<br>" +
("5" / "2") + "<br>"
</script>
</body>
</html>
<p id="demo"></p>
<script>
function myFunction() {
var x, text;
<p>If the number is less than 100 or greater than 300, an error message will be displayed.</p>
<p id="demo"></p>
<script>
function myFunction() {
var inpObj = document.getElementById("id1");
if (!inpObj.checkValidity()) {
document.getElementById("demo").innerHTML = inpObj.validationMessage;
} else {
document.getElementById("demo").innerHTML = "Input OK";
}
}
</script>
</body>
</html>