Wednesday, June 5, 2019

JavaScript



What is JavaScript?
  • JavaScript is a Scripting Language.
  • JavaScript is programming code that can be inserted into HTML pages.
  • All modern HTML pages are using JavaScript. 
  • JavaScript code can be executed by all modern web browsers.    
JavaScript Display Possibilities

JavaScript can "display" data in different ways:
  • Writing into an HTML element, using innerHTML.
  • Writing into the HTML output using document.write().
  • Writing into an alert box, using window.alert().
  • Writing into the browser console, using console.log(). 
Example

<html>
       <body>
                   <h1>My First Example</h1>
                   <p>My First Style</p>
                   <script>
                    document.getElementById("demo").innerHtml= 5+6;   //innerHTML
                    document.write(5+6);                                                //document.write
                    window.alert(5+6);                                                         //window.alert
                    console.log(5+6);                                                           //console.log
                   </script>

        </body>
</html>


JavaScript Keywords

         Keywords                                               Description
  1. break                           Terminates a switch or a loop
  2. continue                       Jumps out of a loop and starts at the top
  3. debugger                     Stops the execution of JavaScript, and calls                                                      the debugging function
  4. do...while                     Executes a block of statements, and repeats                                                    the block, while a condition is true
  5. for                                Marks a block of statements to be executed,                                                    as long a condition is true
  6. function                        Declares a function
  7. if...else                          Marks a block of statement to be executed,                                                     depending on a condition
  8. return                           Exits a function
  9. switch                           Marks a block of statement to be executed,                                                     depending on different cases
  10. try...catch                     Implements error handling to a block of statements
  11. var                                Declares a variable                                                       
JavaScript Comments
  • Single line comments start with //
  • Multi-line comments start with /* and end with */

External JavaScript Advantages
  • It Separates HTML and code
  • It makes HTML and JavaScript easier to read and maintain
  • Cached JavaScript files can speed up page loads
JavaScript Arithmetic Operators

Operator                    Description

      +                   Addition
        -                          Subtraction
                              Multiplication
        **                  Exponentiation
         /                   Division
        %                 Modulus
        ++                 Increment
        --                  Decrement
         &                  AND
          ~                 NOT
       ^                  XOR
         <<                       Zero fill left shift
         >>                Signed right shift
        >>>              Zero fill right shift

Creating an Array
      Using an Array literal is the easiest way to create a JavaScript Array.
         Syntax :
               Type :- 01
                           var array_name=[item1,item2,....]
               Type :- 02
                           var array_name=new Array(item1,item2,....)
Array Elements
  1. Popping - pop() - This method removes the last element from an array.
  2. Pushing - push() - This method adds a new element to an array(at the end).
  3. Shifting - shift() - This method removes the first array element and "shifts" all other elements to alower index.
  4. Unshifting - unshift() - This method adds a new element to an array (at the beginning), and "unshifts" older elements.
  5. Deleting - delete - Since JavaScript arrays are objects, elements can be deleted by using the JavaScript operator delete.
  6.  Splicing - splice() - This method can be used to add new items to an array.
  7. Concatenating - concat() - This method creates to a new array by merging (concatenating) existing  arrays.
  8. Slicing - slice() - This method slices out a piece of an array into a new array.
  9. Sorting - sort() - This method sorts an array alphabetically.
  10. Reversing - reverse() - This method reverses the elements in an array.
  11. Maximum - math.max.apply() - You can use math.max.apply to find the highest number in an array.
  12. Minimum - math.min.apply() - You can use math.min.apply to find the lowest number in an array.  
 JavaScript supports different kinds of loops
  • for - loops through a block of code a number of times
  • for/in - loops through the properties of an object
  • for/of - loops through the values of an iterable object
  • while - loops through a block of code while a specified condition is true
  • do/while - also loops through a block of code while a specified condition is true 

  

















No comments:

Post a Comment

Express

What is Express? Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develo...