Friday, May 19, 2017

JavaScript Data Types

Like many other programming languages, JavaScript has variables. JavaScript has dynamic types. This means that the same variable can be used to hold different data types. JavaScript allows you to hold three data types: numbers, strings, objects and more.

Syntax of JavaScript variable declaration:

var <variable-name>;

Sample Example:

var num = 17; //number
var myname = "labonstack"; //String
var obj = { firstName: "labon", lastName: "stack"  }; // Object
var test = 17 + "labonstack"; // Output "17labonstack"

Note: When adding a number and string then JavaScript will treat the number as a string.

JavaScript expression is evaluated from left to right. All different sequences can produce different results.

For Example 1:

var test = 8 + 2 + "Mynunber"; // 10Mynumber

For Example 2:

var test = "Mynunber" + 8 + 2; // Mynumber82

In the first example, JavaScript treats as a number 8 and 2, until it reaches "My number". 
In a second example, the first operand is a string from the left side so after that all operand considers as a string.

We can declare multiple variables in a variable name like: var firstName, lastName;

JavaScript Variable Scope

JavaScript variables have only two scopes.

Global Variables - A global variable has a global scope which means it can be defined anywhere in your JavaScript code.

Local Variables - A local variable will be visible only to a function where it is defined. Function parameters are always local to that function.

Example of Global and Local Variables scopes:

<title>Example of Example of Global and Local Variables scopes</title>
   <body onload = checkGlobalLocalScope();>
      <script type = "text/javascript">
            var myVar = "global"; // Declare a global variable
            function checkGlobalLocalScope( ) {
               var myVar = "local";  // Declare a local variable

JavaScript Strings

A string is a series of characters like "Labon Stack". Strings are written with quotes. You can use single or double quotes:

var fruitName = "Apple 1kg";   // Using double quotes
var fruitName = 'Grape 2kg';   // Using single quotes

You can use quotes inside a string, as long as they don't match the quotes surrounding the string:

var answer = "It's an amazing";          // Single quote inside double quotes
var answer = "He is called 'John'";    // Single quotes inside double quotes
var answer = 'He is called "John"';    // Double quotes inside single quotes

JavaScript Booleans

Booleans can only have two values: true or false.

var A = true;
var B = false;

Booleans are often used in conditional testing.

JavaScript Numbers

JavaScript has only one type of numbers. Numbers can be written with, or without decimals:

var A = 17.00;     // With decimals point
var B = 17;        // Without decimals point

Extra large or extra small numbers can be written with scientific notation:

var y = 185e5;      // 18500000
var z = 185e-5;     // 0.00185

JavaScript Objects

JavaScript objects are written with curly braces. Object properties are written as name: value pairs, separated by commas.

var student = {FirstName:"Khodidas", LastName:"Diyora", Age:24, City:"Ahmedabad"};

The object student in the example above has 4 properties: FirstName, LastName, Age, and City.

JavaScript Arrays

To using square brackets we can write JavaScript arrays. Array items are separated by commas.

The following code declares an array called fruite, containing four items (fruite names):

var fruite = ["Orange", "Banana", "Apple", "Grape"];

Array indexes are zero-based, which means the first item is [0], second is [1], and so on.

The type of Operator

To using JavaScript type of operator to find the type of a JavaScript variable. The type of operator returns the type of a variable or an expression:

typeof ""                  // Returns "string"
typeof "Labor"              // Returns "string"
typeof "Labon Stack"          // Returns "string"


In JavaScript, a variable without a value has the value undefined. The type of is also undefined.

var fruit;                // Value of fruit is undefined, the type is undefined

Any variable can be emptied, by setting the value to undefined. The type will also be undefined.


In JavaScript null is supposed to be something that doesn't exist.
In JavaScript, the data type of null is an object.

var mynumber = null;         // Value of "mynumber" is null but type is still an object

Empty Values

An empty string variable has both a value and a type and an empty value has nothing to do with undefined. 

var fruite = "";              // The value of fruite is "" and the typeof is "string"

No comments:

Post a Comment