1 - js

Upload: sheeba-dhuruvaraj

Post on 14-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 1 - js

    1/18

  • 7/27/2019 1 - js

    2/18

    JAVA Script

    JavaScript is a scripting language

    JavaScript was designed to add interactivity to HTMLpages

    A scripting language is a lightweight programming

    language JavaScript is usually embedded directly into HTML

    pages

    JavaScript is an interpreted language (means that scripts

    execute without preliminary compilation) Everyone can use JavaScript without purchasing a

    license

  • 7/27/2019 1 - js

    3/18

    JavaScript's official name is ECMAScript.

    ECMAScript is developed and maintained by the ECMAorganization.

    ECMA-262 is the official JavaScript standard.

    The language was invented by Brendan Eich at Netscape (withNavigator 2.0), and has appeared in all Netscape and Microsoft

    browsers since 1996.

    The development of ECMA-262 started in 1996, and the firstedition of was adopted by the ECMA General Assembly in June1997.

    The standard was approved as an international ISO (ISO/IEC16262) standard in 1998.

    The development of the standard is still in progress.

  • 7/27/2019 1 - js

    4/18

    JS can do.

    JavaScript gives HTML designers a

    programming tool

    JavaScript can put dynamic text into an HTML

    page JavaScript can read and write HTML elements

    JavaScript can be used to validate data

    JavaScript can be used to detect the visitor'sbrowser

    JavaScript can be used to create cookies

  • 7/27/2019 1 - js

    5/18

    Syntax:

    ...

    Eg:

    document.write(Welcome to IIST!");

  • 7/27/2019 1 - js

    6/18

    Where JS can be?

    Scripts in Scripts to be executed when they are called, or

    when an event is triggered, go in the head section.

    Scripts in

    Scripts to be executed when the page loads go in

    the body section.

    Scripts in and

    You can place an unlimited number of scripts inyour document, so you can have scripts in both the

    body and the head section.

    External JavaScript

  • 7/27/2019 1 - js

    7/18

    JavaScript Statements

    A JavaScript statement is a command to a

    browser.

    JavaScript is Case Sensitive JavaScript Code - is a sequence of

    JavaScript statements.

    JavaScript Blocks - statements can begrouped together in blocks.

  • 7/27/2019 1 - js

    8/18

    JavaScript Comments

    // Single line comment

    /* Multi - line comment */

    Comments at the end of the line //

  • 7/27/2019 1 - js

    9/18

    JavaScript Variables

    Variable names are case sensitive (a and

    A are two different variables)

    Variable names must begin with a letter or

    the underscore character

    Syntax:

    var x;

    x=5;

    x=IIST;

    var x=10;Note: Variable can be assigned without declaration.

  • 7/27/2019 1 - js

    10/18

    JavaScript OperatorsArithmetic Operators

    + , - , * , / , % , ++ , --Assignment Operators

    = , += , -= , *= , /= , %= ,.=

    Comparison Operators== , != , > , < , =

    Logical Operators

    && , || , !Conditional Operator

    variablename=(condition)?value1:value2

  • 7/27/2019 1 - js

    11/18

    The + Operator Used on Strings

    str1="What a very";

    str2="nice day";

    str3=str1+str2;

    Adding Strings and Numbers

    x=5+5;

    x="5"+"5";

    x=5+"5";

    x="5"+5;

  • 7/27/2019 1 - js

    12/18

    Control Statements

    (i) if statement

    if (condition)

    code to be executed if condition is true;

    (ii) if...else statementif (condition)

    code to be executed if condition is true;

    elsecode to be executed if condition is false;

  • 7/27/2019 1 - js

    13/18

    (iii) If...else if....else statement

    if (condition)

    code to be executed if condition is true;

    else if (condition)

    code to be executed if condition is true;

    else if (condition)

    code to be executed if condition is true;

    elsecode to be executed if condition is false;

  • 7/27/2019 1 - js

    14/18

    (iv) Switch statement

    switch (n)

    {case label1:

    code to be executed if n=label1;

    break;

    case label2:code to be executed if n=label2;

    break;

    default:

    code to be executed if n is differentfrom both label1 and label2;

    }

  • 7/27/2019 1 - js

    15/18

    Loops

    (i) while loop

    while (condition){

    code to be executed;

    }(ii) do While loop

    do

    {code to be executed;

    }while (condition);

  • 7/27/2019 1 - js

    16/18

    (iii) for loop

    for (init; condition; increment)

    {code to be executed;

    }

    (iv) break and continue Statements

    break will terminate the loop

    continue will break the loop for thatiteration and continue with the nextvalue

  • 7/27/2019 1 - js

    17/18

    (v) for - in Statement

    for (variable in object){

    code to be executed

    }

  • 7/27/2019 1 - js

    18/18

    JS Function

    Syntax:

    function functionname(var1,var2,...,varX)

    {some code

    }

    Function call:functionname(var1,var2,,varX);