Thursday 28 February 2013

JavaScript Other Statements



JavaScript : import statement

Description

The import statement is used to import properties, functions, and objects in a script from a signed script that has exported the information.

Version

Implemented in JavaScript 1.2

Syntax

import objectName.name1, objectName.name2, ..., objectName.nameN

import objectName.*

Parameters

objectName : Name of the Object that will receive the imported names.

name1, name2, nameN : List of properties, functions, and objects to import from the export file.

*Imports all properties, functions, and objects from the export script.

Example :

The following web document imports various properties from another script.

HTML Code

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4. <meta charset=utf-8>  
  5. <title>JavaScript import statement :  Example-1</title>  
  6. </head>  
  7. <body>  
  8. <script type="text/javascript">  
  9. // Imports the variables name, class and rollno from another script.  
  10. // Following statements  makes those properties accessible to studentObj  
  11. import studentObj.name;   
  12. import studentObj.class;  
  13. import studentObj.rollno;  
  14. </script>  
  15. </body>  
  16. </html>  

JavaScript : export statement 

Description

The export statement is used to allow a signed script to provide properties, functions, and objects to other signed or unsigned scripts.

Version

Implemented in JavaScript 1.2

Syntax

export objectName.name1, objectName.name2, ..., objectName.nameN

export objectName.*

Parameters

objectName : Name of the Object that will receive the imported names.

name1, name2, nameN : List of properties, functions, and objects to import from the export file.

* : Exports all properties, functions, and objects from the export script.

Example :

In the following web document exports statement export variables class, rollno, and studentDetails function to other signed/unsigned scripts. 
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4. <meta charset=utf-8>  
  5. <title>JavaScript export statement :  Example-1</title>  
  6. </head>  
  7. <body>  
  8. <script type=”text/javascript”>  
  9. // Initialize variables.   
  10. var name = “David Rayy”;  
  11. var class = “II”;  
  12. var rollno =  4;  
  13. function studentDetails()  
  14. {  
  15. alert(“Student Name  is: “ + name);  
  16. }  
  17. //    
  18. Following statement export two variables class, rollno and studentDetails function to other scripts.   
  19. export class, rollno, studentDetails  
  20. </script>   
  21. </body>   
  22. </html>  

JavaScript : return statement

Description

The return statement returns a value and exits from the current function.

Version

Implemented in JavaScript 1.0

Syntax

return expression

Parameters

expression : The expression to return. If not present, function does not return a value.

Example :

In the following web document rectangle_area() function returns the area of a rectangle.

HTML Code

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4. <meta charset=utf-8>  
  5. <title>JavaScript return statement :  Example-1</title&glt;  
  6. <script type="text/javascript"&glt;  
  7. function rectangle_area(h,w)  
  8. {  
  9. return h*w;  
  10. }  
  11. </script&glt;  
  12. </head&glt;  
  13. <body&glt;  
  14. <h1 style="color: red"&glt;JavaScript : return statement</h1&glt;  
  15. <hr /&glt;  
  16. <script type="text/javascript"&glt;  
  17. document.write("Area of the Rectangle " +rectangle_area(400,300)+" sq.ft.");  
  18. </script&glt;  
  19. </body&glt;  
  20. </html&glt;  

Practice the example online


JavaScript : const Statement

Description

A constant is an identifier for a simple value. The value cannot be modified during the script's execution. In JavaScript const statement creates a constants. Constants follow the same scope rules as JavaScript variables.

Syntax

const varname1 = value1 , varname2 = value2,... varnameN = valueN

Parameters

varname1, varname2......varnameN : Constant names.

value1, value2......value3 : Value of the constant.

Example :

The following web document displays the height and width and area of the rectangle.

HTML Code

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4. <meta charset=utf-8>  
  5. <title>JavaScript const statement :  Example-1</title>  
  6. <link rel="stylesheet" type="text/css" href="example.css">  
  7. </head>  
  8. <body>  
  9. <h1>JavaScript : const statement</h1>  
  10. <script src="const-statement-example1.js"></script>  
  11. </body>  
  12. </html>  

JS Code

  1. const height = 400;  
  2. const width = 200;  
  3. var newParagraph = document.createElement("p");  
  4. var newText = document.createTextNode("Height is : "+height+" Width  
  5. is : "+ width +". So, Area of the Rectangle is " + height*width +  
  6. " sq.ft.");  
  7. newParagraph.appendChild(newText);  
  8. document.body.appendChild(newParagraph);  





No comments:

Post a Comment