Javascript Variables and Values:

Variables Data Types Data conversion Special characters

Javascript Variables

Variables are the  basic fundamental building blocks of JavaScript expressions. Variables can store and retrieve data. A variable is a symbolic name that represents some data that you set. A variable can be referred by a name. Certain rules apply when you create a variable name.

A JavaScript identifier or name:

 

  • must start with a letter or underscore (“_”)
  • subsequent characters can also be digits (0-9)

 


Examples : myname, address1,address2,_zipcode are  examples for variable names.

When using a variable for the first time it is not necessary to use “var” before the variable name, but it is a good programming practice to make it crystal clear when a variable is being used for the first time in the program.Please remember that JavaScript is case sensitive. This means that variables Myname and myname  are different variables even though both have same names.

Javascript – Declaring variables

A Javascript variable can be declared in two ways:

 

  • By assigning the variable  a value; Eg: myname = “Bob”;
  • By using keyword var; for example, var myname = “Bob”;


 

 

Variable Scope :
A variable may be have a either global or local scope.

  • Global variable :A global variable is available everywhere in the current document In addition you can access global variables declared in one window or frame from another window or frame by specifying the window or frame name.


Usually a new global variable is created by assigning it a value:

Eg:

<script>
// Example for Global variable
var globalVar1 = “Test value for global variable”;

function ExampleForLocalvariable() {
    var LocalVar1 = “Test value for local variable”; 

    alert(LocalVar1);

}

alert(globalVar1);
</script>

  • Local Variable : A local variable is available only within the function where it was assigned. To create a local variable which has a scope  within a function, the new variable should be declared using the var statement:


Eg:

<script>
var globalVar1 = “Test value for global variable”;

function ExampleForLocalvariable() {
    // Example for local variable
    var LocalVar1 = “Test value for local variable”;
   alert(LocalVar1);

}

alert(globalVar1);
</script>

Top



Javascript – Data Types:
Every Javascript variable can be assigned a value. The value assigned could be a string or a number. JavaScript recognizes the following data types :

Integers Integers can be positive, 0, or negative; Integers can be expressed in decimal (base 10), hexadecimal (base 16), and octal (base 8). A decimal integer literal consists of a sequence of digits without a leading 0 (zero). A leading 0 (zero) on an integer literal indicates it is in octal; a leading 0x (or 0X) indicates hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and AF Octal integers can include only the digits 0-7. Some examples of integer literals are: 24 0xFFF, and -435.
 
Floating-point number A floating-point number can have the following parts: 
1)a decimal integer 
2)a decimal point (“.”) 
3)a fraction (another decimal number) 
4)an exponent 
5)a type suffix 
The exponent part is an “e” or “E” followed by an integer, which can be signed (preceded by “+” or “-“). A floating-point literal must have at least one digit and either a decimal point or “e” (or “E”). 
Some examples of floating-point literals are 3.1415, -3.1E12, .1e12, and 2E-12 
Boolean   True or False. The possible Boolean values are true and false. These are special values, and are not usable as 1 and 0. In a comparison, any expression that evaluates to 0 is taken to be false, and any statement that evaluates to a number other than 0 is taken to be true. 
Strings  “Hello !” Strings are delineated by single or double quotation marks. (Use single quotes to type strings that contain quotation marks.) 
Objects   myObj = new Object(); 
null   Not the same as zero – no value at all. A null value is one that has no value and means nothing. Because JavaScript is case sensitive, null is not the same as Null, NULL, or any other variant. 
Undefined  A value that is undefined is a value held by a variable after it has been created, but before a value has been assigned to it. 

Top




Javascript – Data Type Conversion

Javascript is a NOT a strongly typed language. While declaring a new variable, you do not have to specify the data type of a variable. Data types are converted automatically as needed during script execution.

For example, you could define a variable as follows:

var myvar=20
Later in your script, you may assign the same variable myvar a string value, for example,

myvar = “Welcome home!!”

Top




Javascript – Special Characters :

Character Meaning
\b backspace
\f form feed
\n new line
\t tab
\\ backslash character
\r carriage return

Top

Leave a Comment