Java Reference
In-Depth Information
print("empId = " + empId + ", deptId = " + deptId);
empId = 100, deptId = undefined
empId = 100, deptId = 200
Radius = 2.30, Area = 16.62
empId = 100, deptId = 200
In the code, the variable deptId is declared local to the block. However, you were
able to access it outside (before as well as after) the block. This is not a bug in Nashorn.
This works as designed, based on the variable scoping rules. Variable scoping in Nashorn
works quite differently from that of in Java. I will discuss variable's scoping in the section
Variable Scoping and Hoisting .
Variable Statement
A variable statement is used to declare and, optionally, initialize variables. I have already
discussed the variable statement in the section Declaring Variables . Examples of variable
statements are as follows:
// Declare a variable named empId
var empId;
// Declare a variable named deptId and initialize it to 200
var deptId = 200;
Empty Statement
A semicolon is used as an empty statement. The empty statement in Nashorn works
the same way as that of in Java. It has no effect. It can be used anywhere a statement
is required. Like Java, a for statement in Nashorn is used for iteration purpose. The
following code uses a for statement to print integers 1 through 10. An empty statement is
used as the body of the statement:
// The semicolon at the end is the empty statement
for(var i = 1; i <= 10; print(i++));
Expression Statement
An expression statement is a statement that consists of an expression with/without side
effects. The following are some examples of expression statements:
var i = 100; // A variable statement
i++; // An expression statement
print(i); // An expression statement
 
Search WWH ::




Custom Search