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.*
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.
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
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset=utf-8>
- <title>JavaScript import statement : Example-1</title>
- </head>
- <body>
- <script type="text/javascript">
- // Imports the variables name, class and rollno from another script.
- // Following statements makes those properties accessible to studentObj
- import studentObj.name;
- import studentObj.class;
- import studentObj.rollno;
- </script>
- </body>
- </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.*
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.
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.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset=utf-8>
- <title>JavaScript export statement : Example-1</title>
- </head>
- <body>
- <script type=”text/javascript”>
- // Initialize variables.
- var name = “David Rayy”;
- var class = “II”;
- var rollno = 4;
- function studentDetails()
- {
- alert(“Student Name is: “ + name);
- }
- //
- Following statement export two variables class, rollno and studentDetails function to other scripts.
- export class, rollno, studentDetails
- </script>
- </body>
- </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
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset=utf-8>
- <title>JavaScript return statement : Example-1</title&glt;
- <script type="text/javascript"&glt;
- function rectangle_area(h,w)
- {
- return h*w;
- }
- </script&glt;
- </head&glt;
- <body&glt;
- <h1 style="color: red"&glt;JavaScript : return statement</h1&glt;
- <hr /&glt;
- <script type="text/javascript"&glt;
- document.write("Area of the Rectangle " +rectangle_area(400,300)+" sq.ft.");
- </script&glt;
- </body&glt;
- </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.
value1, value2......value3 : Value of the constant.
Example :
The following web document displays the height and width and area of the rectangle.
HTML Code
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset=utf-8>
- <title>JavaScript const statement : Example-1</title>
- <link rel="stylesheet" type="text/css" href="example.css">
- </head>
- <body>
- <h1>JavaScript : const statement</h1>
- <script src="const-statement-example1.js"></script>
- </body>
- </html>
JS Code
- const height = 400;
- const width = 200;
- var newParagraph = document.createElement("p");
- var newText = document.createTextNode("Height is : "+height+" Width
- is : "+ width +". So, Area of the Rectangle is " + height*width +
- " sq.ft.");
- newParagraph.appendChild(newText);
- document.body.appendChild(newParagraph);
No comments:
Post a Comment