A variable holds an item of data that can change or vary. In many programming languages, a variable can change its value but not its type. In JavaScript, the value and even the type of data can change.
The program below begins with a declared variable named "X" that is undefined. This variable is then assigned the values of an integer, a string, and a boolean. Incidentally, the value "undefined" is a legitimate JavaScript value.
<!DOCTYPE html> <html> <head> <title>XoaX.net's Javascript</title> </head> <body> <script type="text/javascript" src="Variables.js"></script> </body> </html>
// Declare the variable with no value. var X; document.writeln("X = "+ X + "<br />"); // Assign it an integer value. X = 4; document.writeln("X = "+ X + "<br />"); // Assign it a string value. X = "XoaX.net"; document.writeln("X = "+ X + "<br />"); // Assign it a boolean value. X = true; document.writeln("X = "+ X + "<br />");
© 20072024 XoaX.net LLC. All rights reserved.