Examples with typeof :

var y = 1;
  if (function f(){}) {
    y += typeof f;
  }
  console.log(y);

In the above example, the output is 1undefined as the if statement code executes at runtime, and the statement inside the if condition is evaluated during run time.

var k = 1;
  if (1) {
    eval(function foo(){});
    k += typeof foo;
  }
  console.log(k);

output : 1undefined

The if condition evaluates this statement using eval and the eval(function foo(){}) returns true. But as the statement executes at runtime the output will be 1undefined.

var k = 1;
  if (1) {
    function foo(){};
    k += typeof foo;
  }
  console.log(k);

Here, the typeof returns function and the output is :

1function

Pre-increment and Post-increment

The pre-increment and post-increment results the same but the only difference is the result of evaluating the expression.

++i  : increments i and evaluates the new value of i.

i++ : evaluates the old value of i and then increments i.

for(var i=0;i<=10;i++){
console.log(i);
}

for(var i=0;i<=10;++i){
console.log(i);
}

Result for both for loops : 
0
1
2
3
4
5
6
7
8
9
10

Comparing Objects

In JavaScript objects are a reference type. Two distinct objects are never equal, even if they contain same properties. If the same object reference is compared, then it returns true.

For Example :

var veggies = { name : 'potato' };
var vegetable = { name : 'potato' };

Two variables and the two distinct objects with same properties :

veggies == vegetable // returns false;
veggies === vegetable // returns false;

Two variables and a single object :

var veggies = { name : 'potato' };
var vegetable = veggies ;

As the veggies and vegetable are pointing to the same object, it returns true.

veggies == vegetable // returns true;
veggies === vegetable // returns true;

If the name gets updated, then the changes are reflected in the reference object also.

veggies.name = 'carrot';
console.log(vegetable); // returns { name : 'carrot' }




Deleting Properties

A non-inherited property can be removed by using the delete operator.

For Example :

Create a new object, named myObject with two properties :

var myObject = new Object();
myObject.a = 7;
myObject.b = 8;

delete myObject.a;
console.log('a' in myObject) // false (As the object is deleted)

we can also delete a global variable if var keyword is not used to declare a variable.

a = 15;
delete a;

Getters and Setters

A getter is a method that gets the value of a specific property and setter is a method that sets the value of a specific property. The getters and setters method can be defined on any predefined core objects or the user defined objects that supports the addition of new properties.

Example :

var obj = {
a : 7,
     get b(){
          return this.a * 2;
      },
     set c(x){
          this.a =  x/ 5;
      }
};

console.log(obj.a); // 7
console.log(obj.b); // 14
obj.c = 100;
console.log(obj.a); // 20

The obj object properties are :

obj.a ==> a number

obj.b ==> a number multiplied with 2.

obj.a ==> a number sets the value of obj.c/5;

The getters and setters can extend the Date property to add a year property to all the instances of the predefined Date class. It uses getFullYear and setFullYear methods to support the year property's getter and setter.

var a = Date.prototype;
Object.defineProperty(a, 'year', {
  get: function() { return this.getFullYear(); },
  set: function(y) { this.setFullYear(y); }
});

var today= new Date();
console.log(today.year); //2017
today.year = 2001;
console.log(today); //Mon Dec 03 2001 20:45:11 GMT+0530 (India Standard Time)

The getters and setters can be defined in two ways :

  • It can be defined using object initializer.
  • It can be added later to any object at any time using getter or setter method.
when getter and setter methods are defined using object initiallizer, we just need to do is, add a prefix i.e, for getter method with get and setter method with set. The getter method must not take any parameters and setter methods exactly one parameters.

var obj = {
a:7;
get b(){}
set c(x){}
};

Getters and Setters can also be added to an object at any time using the Object.defineProperties method. 

Example :

var obj = { a : 7 };


Object.defineProperties(obj, {
    'b': { get: function() { return this.a + 1; } },
    'c': { set: function(x) { this.a = x / 2; } }
});

obj.c = 10;
console.log(obj.a); // 5
console.log(obj.b); // 6






Difference between object literal notation and JSON

The Object literal notation is not as same as JavaScript Object Notation(JSON).


  • JSON property is defined as "property" : "value". The property cannot be short handed and it must be double quoted.
  • In JSON the values can be only strings,numbers,array,true,false,null or another JSON object.
  • A function can not be assigned to a value in JSON.
  • Objects like date can be a string after JSON.parse().
  • JSON.parse() will not accept computed property names and error will be thrown. 

Object Initializer

JavaScript has number of predefined objects and can also create our own objects. object cane be created using object initializer.

Object Initializer :

Objects can be initialized using new Object(), Object.create() or using initializer notation. It is a comma delimited list of zero or more pairs of property names and associated values of an object, which are be defined in curly braces ({}).

syntax :

var data = {};

var data = {a:'hello',b:42,c:{}};

var a = 'hello', b = 42, c = {};
var data = {a:a,b:b,c:c};

var newData = {
         property: function ([parameters]) {},
         get property() {},
         set property(value) {}
};

where as In ECMAScript 2015, these notations are defined as :

var a = 'hello', b = 42, c = {};
var data = {a,b,c};

var a = {
      property([parameters]){}
};

var prop = 'zoo';

var a = {
     [prop]: 'hello',
     ['b' + 'ut']: 'here'
};

values of the object properties can contain primitive datatypes or other objects.

Creating objects :

An empty object with no properties can be created as :

var emptyObject = {};

The main advantage of this notation intializer is we can create objects quickly with its properties inside curly braces. You can simply notate a key : value pairs which are been separated by comma.

Example :

var details = {
name : 'ram',
age : 20,
college:'srichaitanya'
}

Accessing Properties :

Once the object is defined we might want to retrieve them or change them. Object properties can be accessed using dot notation or bracket notation.

details.name; //ram
details['age'] //20

details.name = 'tommy';

Duplicate property name :

when the same name is used for the property, then the second property is overwritten by the first value.

Example :

var z = {x:1,x:2};

console.log(z); //  x : 2

In ECMAScript 5, if duplicate elements are defined then it would be considered as syntax error.In ECMAScript 2015 has removed this restriction. By using strict mode, it would allow the duplicate property names.

Method Definitions :

A property of an object can refer to a function or getter or setter method.

var example = {
property : function([params]){}
get property() {},
set property(val) {}
};

Spread Properties :

It copies own enumerable properties from a provided object into a new object.

Example :

var obj1 = { foo: 'bar', x: 42 };

var obj = { ...obj1 };

console.log(obj);
// { foo: 'bar', x: 42 }

Overview of AngularJS

AngularJS is an open source web application framework. It is now maintained by Google . The most of the code can be eliminated by using th...