JavaScript

JavaScript Variables and Data Types: Number, String, Boolean, Null

JavaScript variables

JavaScript variables-In any programming language, variables are used to store values during the execution of program (script). JavaScript variables represent a location in the computer’s memory where a value is stored for use in a program. The JavaScript variables are created in memory (RAM) during program execution; therefore the data is stored in them temporarily.

The name of the memory location i.e. the JavaScript variables name remains fixed during execution of the program but the data stored in that location may change from time to time. therefore, the variable is defined as;

“A quantity that might change its value during execution of the program is called the variable”.

Each JavaScript variables is given a name in the program/script code. When the program is executed, different memory locations are allocated to variables of the program. The data is stored (or modified) and read to and from memory with the reference to variable name. All JavaScript variables should be declared with unique names before they can be used in a program.


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Rules For Naming JavaScript Variables:

The variable are word ( or user-defined identifiers) that are named by using a sequence of letters, digits etc. the following rules are followed in JavaScript to give name to a variable:

  • The first character of a JavaScript variables name must be a letter, and underscore character( _ ) or a dollar sign ( $ ) character.
  • Special character, such as arithmetic operators, ^, #, ~, etc (except underscore) cannot be used in a JavaScript variables name.
  • The keyword cannot be used as a JavaScript variables name.
  • Blank spaces cannot be used in JavaScript variables name.
  • A JavaScript variables name declared for one data type, the same name cannot be declared for another data type.
  • The meaningful name should be given to the JavaScript variables ( or identifier).
  • JavaScript is a case-sensitive language, therefore it distinguishes between lowercase and uppercase letters. Thus the JavaScript variables “ABC” and ‘abc’ are two different variable.

The following are some examples of the valid and invalid JavaScript variables names:

Variable name Valid / Invalid Reason for invalidity
4_20a Invalid First character is a digit. JavaScript variables names cannot start with a digit.
_sno Valid
Receipt# Invalid The special character # is not allowed
Plus- Invalid The special character hyphen (-) is not allowed
Ab c Invalid Blank space in JavaScript variables name is not allowed.



Declaring JavaScript Variables:

Defining the JavaScript variables names in the program/script is called declaring variables. In JavaScript, all JavaScript variables must be declared before they used in a script. In JavaScript, a JavaScript variables is declared using the keyword

‘var’

The general syntax to declare a JavaScript variables is:

var  varname;

where

var : it is keyword which is used to declare a variable.

Varname: it represents a valid variable name.

For  example, to declare variables roll_no, name, and address, the statements are written as follows:

Var roll_no;

Var name;

Var address;

In JavaScript, the data types of variable are not specified with declaration of variables. In fact, a variable does not have a fixed data type. The same JavaScript variables can be used to store any type of data at any time. JavaScript assigns to the variable, the data type of the value that you assign. Javascript automatically converts value of one type to another.

Initialization Of JavaScript Variables:

In JavaScript, when a variable is declared, the compiler allocates some memory space for it. A meaningless data (also called garbage data) may be assigned to it. Sometimes, a variable can also be assigned a value by programmer at the time of its declaration.

Assigning a value to a variable at time of its declaration is called initializing a variable.

The general syntax of initializing a JavaScript variables is:

Var variable = value;

Where

Var : it is a keyword of javascript.

Variable: specifies the name of variable to be initialized.

Value: specifies the value to be assigned to the variable.

For example, to declare a variable ‘x’ and to assign value 5 to it, the statement  Is given below:

Var x=  5;

You can initialize a variable with a null value e.g.

Var x = null;

If ‘x’ is used in an expression then javascript interpreter will convert the value null to ‘number’ data type. So value 0 will be assigned to ‘x’.

Let us see the following code:

Var x=null;

Var y= 6;

Var z= x*y;

After execution of the above statements, the value 0 will be assigned to ‘z’.

Please note that if you re-declare a variable in JavaScript, it will not lose its previous value assigned to it e.g.;

Var x= 10;

Var x;

Document.write(“value of x =” +x);

Output of code:

Value of x = 10

Note:If you declare a JavaScript variables without initializing it an undefined value will be assigned to that variable.


Data types:

The data type define the set of values and a set of operations that can be performed on these values. The values or data used in the program may be of different types. JavaScript provides the following data types:

  1. Number
  2. String
  3. Boolean
  4. Object
  5. Null
  6. Undefined

Number:

Number data type consists of numbers or numeric values. Mathematical calculations can be performed on number data type. Numeric values may be whole numbers or floating point numbers(i.e. real numbers)

Therefore, number data type is divided into two types:

  1. Integer:

An integer is a whole number without decimal part, it may have a positive or a negative value. the number 0,1,2,3….., and their negatives -1,-2,-3….., are examples of integers.

In JavaScript, integers can be expressed in decimal , octal, or in hexadecimal from.

  • A decimal (base 10) number based on 10 digits (from 0 to 9) such as 75802 is an example of decimal number. A decimal number never starts with a digit 0.
  • Hexadecimal (base 16) number based on 16 digits that are from 0 to 9 and then from ‘A’ to ‘F’. the value of A is 10, B is 11, C is 12, D is 13, E is 14 and F is 15. The number 2A6BF represents hexadecimal value.
  • An octal (base 8) number base on 8 digits (from 0 to 10) such as 7502 is an example of octal number.

Floating Point JavaScript data type:

The number (or numeric values) that have decimal point in them are called floating point numbers. For example, 0.1, 0.002, 3.1010, 4.0 are floating point number. It must be noted that the 4.0 is floating point number while 4 is an integer number. The floating point number can also be represented in exponential notation. For example,

123.45 can be written as 0.12345×103

0.00012 can be written as 0.12×10-3

The number of digits after the decimal point is called precision. For example, in number 0.12345×103 , 103  the precision is 5. Similarly, in floating point number 0.12345×103 , 103 specifies the range of number.

The storage area occupied by the floating point number is divided into two section.

Mantissa : it is a value of the number.

Exponent: it is the value of exponential power.

For example, the number 6432157 can be represented as 6.432157 x 106, where 6.432157 is the mantissa and 6 is the exponent.

In JavaScript, the floating point number 6.432157×106 can be represent in scientific notation as 6.432157E6. it means that the letter ‘E’ or ‘e’ is used to separate the exponent section from mantissa. If the number is smaller than 1, then exponent would be negative. For example, the number 0.000623 will be represented in computer as: 6.23E-4.


String:

A String is just a sequence of characters treated s single data items, such as your name or address. You can say that a string data type consist of text. The mathematical operations cannot be performed on the string data type. The example of string type data are “012345”, “main road uk”, “john” etc.

Boolean:

in JavaScript, the value true and false indicate the Boolean data. The Boolean JavaScript variables is used to store either true or false value. the Boolean value true can be represented by integer value “1”. Similarly Boolean value false can be represented by integer value “0”. Actually, any non-zero value is considered as true. JavaScript automatically converts the Boolean value true and false into 1 and 0 respectively when they are used in numerical expressions.

Null:

The null value means “no value”. the null value is common to all JavaScript types. It is used to set a variable to an initial value. the null value is initialized to a variable, so that garbage data cannot be assigned to it.

Undefined:

The undefined value indicates that a JavaScript variables has been created but not assigned a value. like ‘null’ , the undefined value is common to all JavaScript data types. It is automatically converted to default values of these types.

Example how declare a JavaScript variables in programming:

<html>
<body>
<script type="text/JavaScript">
    var n;
    for(n=1; n<=10; n++)
        document.write(n+ "<br>");
    document.write("ok");
    
</script>
</body>
</html>

 

JavaScript variables



Example to assign value to JavaScript variables:

<html>
<body>
<script type="text/JavaScript">
    var n;
    n=1;
    while(n<=10)
    {
    document.write(n+"<br>");
    n++;
    }
    document.write("ok");
        
    
</script>
</body>
</html>

JavaScript variables


In the above script “n<=10” is a test condition and value 1 is assigned to variable  ‘n’ before the loop statement to make the condition true. When the condition is nested, it will return true value, so body of loop will be executed. In the body of loop, the “n++”, statement changes the value of variable ‘n’ during program execution. After execution the body o loop 10 times, the value of ‘n’ becomes 11. So the loop is terminated and control shifts to the statement “document.write(“ok”); ” that comes immediately after the ‘while’ loop structure.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button