Tuesday 5 August 2014

Values, Variables in Javascript - The Building Blocks

Data types - Fundamental building blocks of any language is the literals. 


Like C#, Javascript also boasts of value type and object but it goes without saying its not all same.

Value types: Fantastic Five
  1. Numbers: wrapper object to allow working with numeric values.
  2. Boolean: wrapper object over boolean values.
  3. Strings: wrapper over a sequence of characters.
  4. null:a special keyword denoting a null value. Note that Javascript is case sensitive thus null is not equal to NULL.
  5. undefined: as the name suggest if something is not defined then its undefined. Note that null is not same as undefined. To give an example
         var a;
          console.log(a); // undefined.
Apart from these primitive types JS has an object (yeah....) and functions (so what). But in JS an object is fundamentally just a collection of name/value pairs (duh isnt that a JSON object - well hold your horses we will come back later to that).

Hmm so what is a function, well if I have to put in possibly most simple terms functions are the objects which can be executed.

Playing with Data type:

JS is a dynamic typed language, what this means is that you don't have to specify the data type of a type when you declare it. Its the JS interpreter which automatically implies the data type for a variable during the script execution - real nice and cause of our very first confusion.

Since JS is dynamically typed language  thus following statement are valid

var name ="myname";
name = 5;

So if we use + operator with numeric operand it results in a numeric value, however it we use + operator with a numeric and string then JS converts numeric to string.

This paradigm is not followed for other operators for example 

"55"-5 // 50
"55"+5 // 555 -> converting numeric to string

Variables

Declaring variables: We can declare variable in JS in two ways:
  1. Using Var keyword and then assigning the value. This can be used to create local as well as global variables
  2. Without using Var keyword and directly assigning the value. This always creates a global variable
Always declare variables using var keyword (period)

A variable declared using var keyword with no initial value specified has the default value of undefined. Attempt to access an undeclared variable with raise ReferenceError exception.

A variable declared inside a function without var is indeed a global variable.

function foo(){
 name='foo'; //global scope
var age=30; //local scope
}

typeof Operator


Using typeof operator on primitive type returns one of the following:
  1. undefined if the  value is undefined
  2. boolean if value is boolean
  3. string if value is string
  4. number if value is number
  5. object if value is an object (i.e not a function)
  6. function if value is function
Hey but the fantastic five also had null, what about that?
Well calling typeof on null returns object, to think about it null is actually an empty object.

The object type

In JS an object is a collection of name value pair. A new instance of object can be created using new keyword.
var o = new object();  // look its all same as done in C# or Java

This is good for now we will uncover object in detail in coming sections




No comments:

Post a Comment