Page 145 - DCAP408_WEB_PROGRAMMING
P. 145
Unit 5: Scripting Language
document.write(linebreak) Notes
my_var = “I am learning JavaScript!”
document.write(my_var)
document.write(linebreak)
my_var = “Script is Finishing up...”
document.write(my_var)
//—>
</script>
</body>
</html>
Output
Hello World!
I am learning JavaScript!
Script is Finishing up...
5.3.1 Declaring vs. Initializing Variables and Null
To create a variable, the var keyword precedes the variable name, and is used only once for
declaration. All future references to the variable are made without the var keyword.
Variables can be assigned values later on, or immediately by following the name with an equals
sign, then the value they represent. Assigning a value to a variable immediately is known as
initializing the variable.
!
Caution If a variable is not initialized, it has a value of null and has only been declared.
var myVarName // declare variable, has null value
myVarName = 162 // assign a value, null value is replaced
//OR
var myVarName = 162 // declare AND assign value (initialize)
myVarName = “two hundred” // the value can be changed later on, even to a different type of
value
var time,dog,baby //multiple variables may be declared simultaneously
var time = 1, dog = “hairy”, baby = true //multiple variables may be initialized simultaneously
5.3.2 JavaScript Variable Naming Conventions
When choosing a variable name, you must first be sure that you do not use any of the JavaScript
reserved names found here. Another good practice is choosing variable names that are descriptive
of what the variable holds. If you have a variable that holds the name of a month, then name it
“month_name” to make your JavaScript more readable.
Finally, JavaScript variable names may not start with a numeral (0-9). These variable names
would be illegal: 7lucky, 99bottlesofbeer, and 3zacharm.
LOVELY PROFESSIONAL UNIVERSITY 139