Variable types
Javascript handles the following variable types:
1. Number (Integer and Float)
2. String
3. Boolean
In javascript all variable types are declared as var. Only depending on how we assign the value.
1. var x=5; // This means 5 is integer.<br>
2. var x="5"; // This means "5" is string.<br>
3. var x=false; // This means "false" is boolean<br>
1.Variable type integer
2.Variable type string
3.Variable type boolean
1.Variable type integer
Integer variables are used to store integer values like 456, 157 etc. To declare a variable of type integer, type any word like “num” followed by the “var” keyword. You can give any name to a variable.
For integer variable type, the value assigned should not be given in quotes. If a number is not quoted, then that number is considered as a integer and not as an string. if var a = 1; and var b = 1; then a+b will give 2 not "11".
Example: 1
var num=20;
document.write(name);
</script>
Example: 2
var num=(20*5+10/2);
document.write (name);
</script>
Example: 3
var num1=20;
var num2=30;
var answer=(num1+num2);
document.write(answer);
</script>
2.Variable type string
For string variable type, the value assigned should be given in quotes. If a number is quoted, then that number is considered as a string and not as an integer. if var a = "1"; and var b = "1"; then a+b will give "11" not 2.Example: 1
var name="Aspell Systems";
document.write(name);
</script>
Example: 2
var name="Aspell";
document.write (name);
name="Systems";
document.write (name);
</script>
3.Variable type boolean
In Boolean logic, a statement can have two values, true or false.
No quotes needed.
Boolean variable values are mostly used along with"if", "if else" statements and sometimes with "while" loops.
Example
var a = true;
if(a == true)
{
document.write("This line will be executed and shown in browser");
}
</script>
No comments:
Post a Comment