Scripting in Java Tutorial - Scripting in Java Variables

Scripting languages are loosely typed.

The type of a variable is not known at compile-time. The type can change during the execution of the program. and is determined at runtime based on the value stored in the variable.

In Nashorn, the keyword var is used to declare a variable.

// Declare a variable named a
var a;

Unlike Java, we don't specify the data type in Nashorn.

We can declare multiple variables in one variable statement.

The variable names are separated by a comma:

// Declare three variables
var a, b, c;

A variable is initialized to undefined when declared.

We can initialize a variable with a value at the time of declaration:

var a = 400, b = ["x", "y", "z"];

In nonstrict mode, you can omit the keyword var in the variable declaration:

// Declare a variable named a without using the keyword var
a = "xyz";