Java Script
Java Script
• if-else
• nested-if
• if-else-if
if:
• if statement is the most simple decision
making statement.
• It is used to decide whether a certain
statement or block of statements will be
executed or not
• else {
• // Executes this block if // condition is false
• }
Flow chart
example
• <html>
• <head>
• <title>If...Else Statments!!!</title>
• <script type="text/javascript">
• // Get the current hours
• var hours = new Date().getHours();
• if(hours<12)
• document.write("Good Morning!!!<br />");
• else
• document.write("Good Afternoon!!!<br />");
• </script>
• </head>
• <body>
• </body>
nested-if:
• A nested if is an if statement that is the target of
another if or else. Nested if statements means an if
statement inside an if statement.
• if (condition1) {
• // Executes when condition1 is true
• if (condition2)
• {
• // Executes when condition2 is true
• }
• }
Flow chart
• <script type = "text/javaScript">
•
• // JavaScript program to illustrate nested-if statement
•
• var i = 10;
•
• if (i == 10) {
•
• // First if statement
• if (i < 15)
• document.write("i is smaller than 15");
•
• // Nested - if statement
• // Will only be executed if statement above
• // it is true
• if (i < 12)
• document.write("i is smaller than 12 too");
• else
• document.write("i is greater than 15");
• }
• < /script>
if-else-if ladder:
• Here, a user can decide among multiple
options.The if statements are executed from
the top down.
• if (condition)
• statement;
• else if (condition)
• statement; . .
• else statement;
• <script type = "text/javaScript">
• // JavaScript program to illustrate nested-if statement
•
• var i = 20;
•
• if (i == 10)
• document.wrte("i is 10");
• else if (i == 15)
• document.wrte("i is 15");
• else if (i == 20)
• document.wrte("i is 20");
• else
• document.wrte("i is not present");
• < /script>
Switch case
• The switch case statement in JavaScript is also
used for decision making purposes.
• default: statementDefault;
• }
• <script type = "text/javascript">
•
• // JavaScript program to illustrate switch-case
•
• var i = 9;
•
• switch (i)
• {
• case 0:
• document.write("i is zero.");
• break;
• case 1:
• document.write("i is one.");
• break;
• case 2:
• document.write("i is two.");
• break;
• default:
• document.write("i is greater than 2.");
• }
•
• </script>
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
Syntax
• delete myAirplane.model;
• Another Loop Statement (an
iterator)for (identifier in object) statement or
compound
• var arrayname=[value1,value2.....valueN];
Ex program
• <script>
• var emp=["Sonoo","Vimal","Ratan"];
• for (i=0;i<emp.length;i++){
• document.write(emp[i] + "<br/>");
• }
• </script>
Output of the above example
• Sonoo
Vimal
Ratan
2) JavaScript Array directly (new
keyword)
• The syntax of creating array directly is given
below:
•
3) JavaScript array constructor (new
keyword)
• Here, you need to create instance of array by
passing arguments in constructor so that we
don't have to provide value explicitly.
• <script>
• var emp=new Array("Jai","Vijay","Smith");
• for (i=0;i<emp.length;i++){
• document.write(emp[i] + "<br>");
• }
• </script>
Output of the above example
• Jai
Vijay
Smith
JavaScript Array Methods
alert(stuff[2][1]);
output
Javascript Array Sort: Sorting Arrays in
Javascript
• To sort an array in javascript, use
the sort() function.
• You can only use sort() by itself to sort arrays
in ascending alphabetical order;
• if you try to apply it to an array of numbers,
they will get sorted alphabetically.
• var fruits = ['apple', 'orange', 'banana'];
• var numbers = [10, 20, 2, 3, 0, 500];
• fruits.sort();
• numbers.sort();
• // We defined show array earlier.
show_array(fruits);
• show_array(numbers);
output
• // Sort in ascending numerical order. numbers.sort(function(a, b)
• {
• if(a > b)
• {
• return 1; }
• else if(a < b)
• {
• return -1; }
• else {
• return 0; } });
• // We defined show array earlier.
• show_array(numbers);
output
JavaScript Functions
• <script>
• function msg(){
• alert("hello! this is message");
• }
• </script>
• <input type="button" onclick="msg()" value="
call function"/>
Output of the above example
Ex:2
• <html>
• <head>
• <script type = "text/javascript">
• function sayHello(name, age) {
• document.write (name + " is " + age + " years old."); }
• </script> </head>
• <body>
• <p>Click the following button to call the function</p> <form>
• <input type = "button" onclick = "sayHello('Zara', 7)" value = "Say
Hello">
• </form>
• <p>Use different parameters inside the function and then try...</p>
</body> </html>
Output
javaScript Function Object
• <script>
• var add=new Function("num1","num2","retur
n num1+num2");
• document.writeln(add(2,5));
• </script>
• Output:7
Ex:2
• <script>
• var pow=new Function("num1","num2","retur
n Math.pow(num1,num2)");
• document.writeln(pow(2,3));
• </script>
• Output:8
Constructors
• Constructors are like regular functions, but we
use them with the new keyword. There are
two types of constructors:
• built-in constructors such as Array and Object,
which are available automatically in the
execution environment at runtime;
• and custom constructors, which define
properties and methods for your own type of
object.
• A constructor is useful when you want to create
multiple similar objects with the same properties
and methods.
• Syntax:
• function Book()
• {
• unfinished code
• }
• var myBook = new Book();
• <html>
• <body>
• <p id="demo"></p>
• <script>
• // Constructor function for Person objects
• function Person(first, last, age, eye) {
• this.firstName = first;
• this.lastName = last;
• this.age = age;
• this.eyeColor = eye;
• }
• // Create a Person object
• var myFather = new Person("John", "Doe", 50, "blue");
• // Display age
• document.getElementById("demo").innerHTML =
• "My father is " + myFather.age + ".";
• </script>
• </body>
• </html>
output
• JavaScript Object Constructors
• My father is 50.
Adding a Property to a Constructor
• <p id="demo"></p>
• <script>
• // Display nationality
• document.getElementById("demo").innerHTML =
• "The nationality of my father is " + myFather.nationality;
• </script>
• </body>
• </html>
output
• JavaScript Object Constructors
• You cannot add a new property to a
constructor function.
• The nationality of my father is undefined
Adding a Method to a Constructor
• <html>
• <body>
• <p id="demo"></p>
• <script>
• </script>
• </body>
• </html>
output
• JavaScript Object Constructors
• My father is John Doe
Built-in JavaScript Constructors
• /pattern/modifiers;
• Ex:
var patt = /w3schools/i;
• Example explained:
• /w3schools/i is a regular expression.
• w3schools is a pattern (to be used in a search).
• i is a modifier (modifies the search to be case-
insensitive).
Using String Methods
• <p id="demo"></p>
• <script>
• var str = "Visit W3Schools!";
• var n = str.search("W3Schools");
• document.getElementById("demo").innerHTML = n;
• </script>
• </body>
• </html>
output
• JavaScript String Methods
• Search a string for "W3Schools", and display
the position of the match:
• 6
Using String search() With a Regular
Expression
• <html>
• <body>
• <p>Search a string for "w3Schools", and display the position of the match:</p>
• <p id="demo"></p>
• <script>
• var str = "Visit W3Schools!";
• var n = str.search(/w3Schools/i);
• document.getElementById("demo").innerHTML = n;
• </script>
• </body>
• </html>
Use String replace() With a Regular
• <html>
Expression
• <body>
• <script>
• function myFunction() {
• var str = document.getElementById("demo").innerHTML;
• var txt = str.replace(/microsoft/i,"W3Schools");
• document.getElementById("demo").innerHTML = txt;
• }
• </script>
• </body>
• </html>
example
• JavaScript Regular Expressions
• Replace "microsoft" with "W3Schools" in the
paragraph below:
• Try it
• Please visit Microsoft and Microsoft!
Regular Expression Modifiers
Modifiers can be used to perform case-insensitive more global
searches :
Regular Expression Patterns
Brackets are used to find a range of characters:
DHTML
• Dynamic HTML can be used to enchance the
interactivity of a web page.
• The interactivity is at the client end without
any extra transactions to the server.
• DHTML uses four different technologies.
• .fixed
• {
• position: fixed;
• background: #cc0000;
• color: #ffffff;
• padding: 30px;
• top: 50;
• left: 10;
• }
• span
• {
• padding: 5px;
• border: 1px #ffffff dotted;
• }
2. Static
• .static
• {
• position: static;
• background: #cc0000;
• color: #ffffff;
• padding: 30px;
• }
• span
• {
• padding: 5px;
• border: 1px #ffffff dotted;
• }
3. Relative
• .relative
• {
• position: relative;
• background: #cc0000;
• color: #ffffff;
• padding: 30px;
• }
• span
• {
• padding: 5px;
• border: 1px #ffffff dotted;
• }
4. Absolute
• .absolute
• {
• position: absolute ;
• background: #cc0000;
• color: #ffffff;
• padding: 30px;
• font-size: 15px;
• bottom: 20px;
• right: 20px;
• }
• .relative
• {
• position: relative;
• background: #aad000;
• height: 300px;
• font-size: 30px;
• border: 1px solid #121212;
• text-align: center;
• }
• span
• {
• padding: 5px;
• border: 1px #ffffff dotted;
• }
• pre
• {
• padding: 20px;
• border: 1px solid #000000;
• }
5. Sticky
• .sticky
• {
• position: sticky;
• background: #cc0000;
• color: #ffffff;
• padding: 30px;
• top: 10px;
• right: 50px;
• }
• span
• {
• padding: 5px;
• border: 1px #ffffff dotted;
• }
• pre
• {
• padding: 20px;
• border: 1px solid #000000;
• }