Before using a variable in a JavaScript program, you must declare with the var keyword
#example.html
<script type = "text/javascript">
//create variable and store data
var x = 10;
var y = 20;
var total = x + y;
document.write(total);
//declare multiple variables
var name, city, email;
name = "Rathorji";
city = "Mumbai";
email = "test@mail.com";
document.write(name + "<br>");
document.write(city + "<br>");
document.write(email + "<br>");
</script>
Phone, x, y and total is the variable name(container) in above program, container can store value
Storing a value in a variable is called variable initialization. You can do variable initialization at the time of variable creation or at a later point when you need that variable.
Output:
30Rathorji
Mumbai test@mail.com |
JavaScript allows you to work with 3 data types −
- Numbers:. 15, 44.2 etc.
- Strings: "Hello world!" etc.
- Boolean: true or false.