Javascript variables are named "holding containers" that can store values (a number, a text string or an object). A variable's value can change during the script.Each variable has a name.
The rules for naming javascript variables are:
The javascript names below are examples of valid variable names:
MySite
_mysite
mySite1
m
The javascript statement in the code below shows you how to assign a value to a variable.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Web Development Tutorials.com</title>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
var car = honda;
</script>
</body>
</html>
You can also assign a value to a variable without the "var" statement like this:
<script type="text/javascript">
car = honda;
</script>
The code below shows how to declare a javascript variables and then display the value.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Web Development Tutorials.com</title>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
var car = "Honda";
document.write(car);
</script>
</body>
</html>
The code above creates the text below.
Use the text editor below to practice what you have learned.
The code below shows how to change the value of a variable.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Web Development Tutorials.com</title>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
var car = "Honda";
document.write(car);
document.write("<br />");
var car = "Acura";
document.write(car);
</script>
</body>
</html>
The code above creates the text below.