- JavaScript Variables
variable names must begin with a letter or underscore ("_")
variables can also start with the $ and _ symbols (although we do not recommend this)
variable names are case sensitive (y and y are different variables)
variables can contain numbers, from A to Z uppercase and lowercase letters
Tips: Both JavaScript statements and JavaScript variables are case-sensitive .
Var A;
"var"-the keyword used to declare a variable
"a"-Variable name
Declaring and initializing variables at the same time
var a= 10;
Declaring multiple variables
var x, y, z = ten; ( only z has This value , the rest is undefined)
- JavaScript Data Types
JavaScript has a dynamic type
JavaScript has a dynamic type. This means that the same variables can be used for different types:
var x//x is undefined
var x = 6; X is a number
var x = "Bill"; X is a string
(1) JavaScript string
strings are stored characters (such as The variable of "Bill Gates").
The string can be any text in quotation marks. You can use single or double quotation marks:
var carname= "Bill Gates";
var carname= ' Bill Gates ';
(2) JavaScript numeric type
JavaScript has only one numeric type. Numbers can be with decimal points or without:
var x1=34.00; use a decimal point to write
var x2=34; do not use the decimal point to write
Large or small numbers can be written by means of scientific (exponential) notation:
var y=123e5; 12300000
var z=123e-5; 0.00123
JavaScript Boolean
(3) a Boolean (logic) can have only two values:true or false.
- Undefined and Null
Undefined This value indicates that the variable does not contain a value.
you can empty a variable by setting the value of the variable to null .
For example:
<script>
var person;
var car= "Volvo";
document.write (person + "<br/>");
document.write (car + "<br/>");
var car=null
document.write (car + "<br/>");
</script>
- Declaring variable types
when declaring a new variable, you can use the keyword "New" to declare its type:
var carname=new String;
var x= new number;
var y= new Boolean;
var cars= new Array;
var person= new Object;
JavaScript variables are objects. When you declare a variable, a new object is created.
Note: all things in JavaScript are objects: strings, numbers, arrays, dates, and so on.
You can also create your own objects.
This example creates a name named "Person" and adds four properties to it:
<script>
Person=new Object ();
Person.firstname= "Bill";
Person.lastname= "Gates";
person.age=56;
Person.eyecolor= "Blue";
document.write (Person.firstname + "is" + Person.age + "Years");
</script>
Variable declarations and data types in JavaScript