Sunday, 6 December 2020

 Variable Declaration and Initialization are separate

Declaration and initialization are separate terms. Declaration means to declare a variable with var keyword which assigns the memory allocation to store the variable, and initialization means assign the initial value to the variable before using it.

 simple example of variable declaration is:


 In the above example, we declare the variable 'y' but had not assigned any values to it, as a result it gives output as:



After assigning some value as in the example:




The output of the above example is:






Reference Error and Undefined

Reference error and undefined are two different things. Reference error arises when we try to use the variable without declaring it.

Below example shows the reference error:



        Error encountered as:




Undefined arises when we declare a variable but had not assigned any value. Following example clear the undefined concept:


 
the output of above example is:




Data Types in JavaScript

Data Types basically specifies what kinds of data are stored by a variable in the program. JavaScript provides different data types.
  • Object: represent complex data.
  • Null: represent unknown value.
  • Array: represent similar value.
  • String: represent string values.
  • Number: represent integer or floating value.
  • Boolean: represent true or false.
  • Undefined: represent undefined value.
In JavaScript, we do not need to explicitly define data types. They are automatically assigned to the variable as per the value they posses during run time.

more clear concept about data types through following example:




  





JavaScript is a Prototypical Language

JS is not a class based language but a prototype based language. A prototyped based language means any object can be assigned as the prototyped for another object. It uses classless inheritance programming style in creating duplicate object.

Let's understand more from basic example:



In the above example, there is a function Student which has two properties as firstName and lastName. we add another property faculty through prototype. Here we create two objects student1 and student2 which inherit the properties of Student.


       Output:

       

Creating Objects in JavaScript

There are two ways to creating the objects in JavaScript.
  1. By object literals
  2. By using object constructor   
The constant values that are assigned to the variable are called JavaScript literals.

In object literals we create and define object value separated by comma inside of curly braces. object literal can created by using following syntax:
  
    object={property1:value1,property2:value2,.....,propertyN:valueN}

A constructor is a method that instantiate an object.

In object constructor, we create a constructor function with properties. Each properties value need to assigned in the current object by this keyword. Object constructor can be created by using given syntax:
 
    function function_name(property1,property2,....propertyN)  {
     this.property1=property1;
     this.property2=property2;
      this.propertyN=propertyN;
  }
var object=new function_name(value1,value2,....valueN);

More clearly understand creation of object by following example:



Output becomes:



No comments:

Post a Comment