In Association with Amazon.in   Flipkart

Tuesday, July 9, 2013

Javascript Basic Script With Examples

variable :  A variable is a user defined name which is used to store constant values in it. Variable is a name which is given to memory location.

In Javascript variable Declaration to Datatypes is not mandatory.

Data Types (Type of Constant Values )

1.    Number (int – 4 bytes , float – 8 bytes , Double 16 bytes)
2.    String     ('*', 'abc'  or  "abc")
3.    Boolean   (True  or False)

Data Type Variables:

Example
               Var a;
                a = 'W3C';
---(or)---

                Var a = 'W3C';
---(or)---
                a = 'W3C';

Points to remember for naming the variables


  •     First char of var name must be in A-Z (or) a-z (or) _ (underscore).
  •     Spaces are not allowed.
  •     Avoid built in functions and reserve words as variable names.


String 
                 
1)  Use   \  when using a quotes " or Apostrophe inside a string.
        Ex :
                    "Hai \" I am Robert"
        
        Note :   \ indicates to browser that coming char is not a special char.

2) \n  is for next line
        Ex  :
                    A = "This is an \n example"

                    A = "This is an\\n example"

Boolean
 
            Boolean a = true;
            Boolean a = false;

Note : don't use quotes like 'true'


Example:

<script type="text/javascript">
        document.write("<h2>Welcome To \"JS\"</h2>");
        a=123;
        b='hello';
        c=5>3;
        document.write("a=" + a + "<br/>b=" + b + "<br/>c=" + c);
        /*  js comment  */
        x=6; y=4;
        document.write("<br/>sum=" + (x+y));
        document.write("<br/>Type of b is " + typeof(b));
       
        per=57.86948775467656;
        document.write("<br/>Per=" + per.toFixed(2));  //  per=57.87
        document.write("<br/>Per=" + per.toFixed(0));  //  per=58
        document.write("<br/>Per=" + Math.round(per));  //  per=58
        document.write("<br/>Per=" + Math.floor(per));  //  per=57
        document.write("<br/>Per=" + Math.ceil(per));  //  per=58
       
               
    </script>

To get the data type of var 

 
                          a = 10;
                          b = typeof(a);          ( it is Expression and function )
                          document.write(b);

Example-2

<html>
<head>
          <script>
           a = 10;
           b = typeof(a);       
           document.write( b );
           </script>
</head>
<body>
</body>
</html>

Note : typeof() is a function as well as expression.

No comments:

Post a Comment


Related Posts Plugin for WordPress, Blogger...