Posts

Showing posts from July, 2018

equals to(==) and exactly equals to(===) operator

/*distinction between ==,=== or !=,!== In JS, some literals falsy in nature, it means their nature is to prove false: undefined, null, false, "", 0, or NaN are falsy in nature, rest other types like number(except 0),string ,any object,function,array are truthy in nature. For example one can check if a variable is undefined(not initialized with any value): var vbn; now to check if is not null or undefined: either one can use if(vbn!=null || vbn != undefined){doSomething} or simple since null and undefined are falsy in nature above condition can be written like: if(vbn){doSomething} internally JS will eveluate like: if(!(undefined||null)) => if(!(falsy||falsy)) = > if(!falsy) Another example : lets say an object having a property with truthy value (not in undefined, null, false, "", 0, or NaN) var objj = { propX : "ValX"}; and we want to execute a condition that if propX really exists then do something: if(objj.propX){do...

JS prototype

/*******************prototype******************* * You may find prototype confusing while reading JS * One having an understanding of Java superclass will be easily able to correlate * prototype in JS is just a super class. * If one says o is a protoype to p, it means the object p extends o(super class java). * or can be interpreted as : o is a blueprint while p is the object made from blueprint o * ok having said that, let dive deep into it: * we already know function is javascript are the first class object or simply a class constructor (like java) * for instance: */ function MyFuncClass() { //you may assume MyFuncClass a name of a class this .myVarA = "first" ; this .myVarB = "second" ; } //now similar to java, if we want this JS Class MyFuncClass to inherit some properties defined by its super class //then we need to define the prototype of this class MyFuncClass. //to define the prototype , one can define a ne...

JS Object

/* *object in js is an entity holding key and value pairs *key will be a valid js variable name(no special charater) *below rules applies, see numObj variable */ var numObj = { 1 : "a" , //here key is 1 (a number) which is acceptable but not recommended 45 : "t" , //1m : "we",//js says if key start with no, it has to be number only m1: "he" , //while alplhabet followed by number is fine _12m: "so" //_ can be followed by alphnumeric character }; //while it is recommended in JS to access any object key by using dot to access value corresponds to a key //like numObj.m1, numObj._12, although can also be referenced by numObj["m1"],objNum["_12"] but not recormmended //console.log(numObj.45);//exception : js complain about it not a valid expression to access value //numeric key cannot accessed using dot console.log(numObj[ "1" ]); //will print "a" ...

JS Array

//Foreword, this article was written in haste, please forgive for formatting of article, //but I am sure you will take away something. //Array /** * An Array is a collection of items, usually it is a collection of similar type of objects * which is true for a typed safe language like Java,C# etc, but js is not a strictly typed language * JS array can hold heterogenous items. */ //JavaScript also has a second value that indicates absence of value. //for example var myVar; and if we print this variable then //it will print undefined, because variable has no value assigned. var arr = [ 3 , { a: "itm1" , b: "itm2" }, 7 , function () { console.log( "under func" ); }]; //above array arr is an heterogenous item array, it compose of numbers 3,7 and object {a:"itm1",b:"itm2"} // also it holds a reference to function object. console.log(arr[ 0 ]); //will print 3. console.log(arr[ "2" ]); //wi...