Sunday, December 23, 2018

JavaScript



Date Formate

Date Format has 3 type
  • ISO date
  • Short date
  • Long date

Statement

JavaScript statements are composed of
  • Values
  • Operators
  • Expressions
  • Keywords
  • Comments

Statement Type
var a, b, c; // Declare 3 variables
a = 5; // Assign the value 5 to a
b = 6; // Assign the value 6 to b
c = a + b; // Assign the sum of a and b to c



Boolean


  • JavaScript has a Boolean data type.
  • It can only take the values true or false.



Operator Description Example
== equal to if (day == "Monday")
> greater than if (salary > 9000)
< less than if (age < 18)


Object

  • Object can be store 3 type of datas.
var objectName= new Object();

Example :

<html>
   <head>
   <title>User-defined objects</title>
 
      <script type="text/javascript">
         // Define a function which will work as a method
         function addPrice(amount){
            with(this){
               price = amount;
            }
         }
       
         function book(title, author){
            this.title = title;
            this.author  = author;
            this.price = 0;
            this.addPrice = addPrice; // Assign that method as property.
         }
      </script>
   
   </head>
   <body>
 
      <script type="text/javascript">
         var myBook = new book("Perl", "Mohtashim");
         myBook.addPrice(100);
       
         document.write("Book title is : " + myBook.title + "<br>");
         document.write("Book author is : " + myBook.author + "<br>");
         document.write("Book price is : " + myBook.price + "<br>");
      </script>
   
   </body>
</html>


Out put
Book title is : Perl 
Book author is : Mohtashim 
Book price is : 100

Events

  • some common HTML events
Event Description
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an HTML element
onmouseout The user moves the mouse away from an HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page

Arrays

  • arrays are used to store multiple values in a single variable
  • What is an Array?
    • array is a special variable
    • Array can hold more than one value at a time

Creating an Array

  • var cars = new Array("Saab", "Volvo", "BMW");
  • var cars = [  "Saab",  "Volvo",  "BMW"];

Access the Elements of an Array

  • var cars = ["Saab", "Volvo", "BMW"]; 
  • document.getElementById("demo").innerHTML = cars[0]; 
Output is Saab

Math Object

Math.round(4.7); returns 5
Math.pow(8, 2); returns 64
Math.sqrt(64); returns 8
Math.abs(-4.7); returns 4.7
Math.ceil(4.4); returns 5
Math.floor(4.7); returns 4
Math.sin(90 * Math.PI / 180); returns 1
Math.cos(0 * Math.PI / 180); returns 1
Math.min(0, 150, 30, 20, -8, -200);
returns -200
Math.max(0, 150, 30, 20, -8, -200);
returns 150
Math.random();
returns a random number

Booleans

Boolean Values

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

Comparisons and Conditions

less than
== equal to if (day == "Monday")
> greater than if (salary > 9000)
< if (age < 18)

Functions

  • When an event occurs (when a user clicks a button)
  • When it is invoked (called) from JavaScript code
  • Automatically (self invoked)

Function Return

  • JavaScript reaches a return statement, the function will stop executing

No comments:

Post a Comment