var a = 5;
var b = 10;
var c = 15;
var d = true;
var e = false;
var result1 = (a < b) && (b < c);
var result2 = d && e;
var result3 = d || e;
var result4 = e || (d && false);
result1 is true, result2 is false,
result3 is true, result4 is false.
var a = 5;
var b = 10;
var c = 15;
var result1 = a + b * c;
var result2 = (a + b) * c;
var result3 = b / a + c;
result1 is 155 (5 + (10 * 15)), result2
is 225 ((5 + 10) * 15), result3 is 17 ((10 /5) + 15).
A form of assignment (initialization) is actually demonstrated in all the above examples.
var x = 5;
x is 5.
var x = 2;
var y;
y = x + 10;
x = x + y;
y is 12, x is 14.
var a = 10;
var b = 15;
var max;
if(a > b)
max = a;
else
max = b;
max is 15.
var n = 5;
var accum = 1;
while(n > 0) {
accum = accum * n;
n = n - 1;
}
accum is 120 (5 factorial).
var Employee =
{
id : 1,
newEmployee : function( first, last, age )
{
var nextId = id;
id = id + 1;
return { id : nextId,
first : first,
last : last,
age : age + 2,
status: "good",
toString : function()
{
return first + " " + last + " has id: " + id +
" and is " + age + " years old. This employee's" +
" status is " + status;
}
};
}
};
var one = Employee.newEmployee( "Paul", "Lorenz", 28 );
var two = Employee.newEmployee( "Bob", "Smith", 32 );
print one.toString();
print two.toString();
var e3 = Employee.newEmployee( "Paul", "Lorenz", 28 ).toString();
var e4 = Employee.newEmployee( "Bob", "Smith", 32 ).toString();
print e3;
print e4;
Paul Lorenz has id: 1 and is 30 years old. This employee's status is good Bob Smith has id: 2 and is 34 years old. This employee's status is good Paul Lorenz has id: 3 and is 30 years old. This employee's status is good Bob Smith has id: 4 and is 34 years old. This employee's status is good