Variables are the containers that are used to store some value. Those values can be changed. Variables are actually the name of a memory location.
Example: var myVariable;
You can define more than one variable in one line.
Example: var first, second;
- It should not contain space.
- It should not begin with a number.
- Variable names are case sensitive.
Example: var myVariable, var myvariable, var my_variable and var myvariable1 are all legal and are all different variables.
Variables can be declared first and then it can be initialized with a value.
myNumber = 10;
Also variables can be declared and initialized in a single line.
var myNumber = 10;
|boolean||Possible values are true(1) or false(0).||true|
|number||it can be an integer or floating point number.||8 or 12.4 or 3.14|
|function||it can be a predefined or user defined function.||write() method|
|object||it can be a predefined or user defined object.||document|
Contatenation operation means joining two or more strings. It can be performed using plus(+) sign.
var str = "a" + "b"; //output: ab var x = "Hello "; var y = "World"; x = x + y; //output: Hello World
x = x + y can also be written as x += y.Example:
<!DOCTYPE html> <html> <head> <title>Variables and Data Types</title> </head> <body> <div id="content"></div> <script> var bool = true; var num1 = 10; var num2 = 12.4; var str; str = "this is my text"; document.getElementById('content').innerHTML = "<h2>Varibales and Data Types</h2>"; document.getElementById('content').innerHTML += "Boolean" + bool; document.getElementById('content').innerHTML += "<br />Integer" + num1; document.getElementById('content').innerHTML += "<br />Real Number" + num2; </script> </body> </html>