<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community:  Bishnu Prasad Chowdhury</title>
    <description>The latest articles on DEV Community by  Bishnu Prasad Chowdhury (@bishnucit).</description>
    <link>https://dev.to/bishnucit</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F556458%2Fde13b0c9-2606-4c29-842a-1735ee473d32.png</url>
      <title>DEV Community:  Bishnu Prasad Chowdhury</title>
      <link>https://dev.to/bishnucit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bishnucit"/>
    <language>en</language>
    <item>
      <title>Memoization in JavaScript</title>
      <dc:creator> Bishnu Prasad Chowdhury</dc:creator>
      <pubDate>Wed, 17 Mar 2021 16:37:40 +0000</pubDate>
      <link>https://dev.to/bishnucit/memoization-in-javascript-29fo</link>
      <guid>https://dev.to/bishnucit/memoization-in-javascript-29fo</guid>
      <description>&lt;p&gt;Memoization is a process by which a recursive function call can be improved more like creating an index for already computed values.&lt;/p&gt;

&lt;p&gt;Before doing a costly function call where it computes some value an index function is called where all previous outputs of the function are stored. If the required calculation is already present in the existing index it returns the result instead of wasting the resource to calculate the result again.&lt;/p&gt;

&lt;p&gt;It is kind of an optimization using which we can save resources and time to compute.&lt;/p&gt;

&lt;p&gt;This is achievable because of closure in JavaScript, we can return the indexed result instead of calculated result using an inner function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// A simple function

var a = (x) =&amp;gt; (x * 2)
a(2);
&amp;gt;4

// Now let's memoize the func

var mem_a = () =&amp;gt; {
    var index = {};
    return (x) =&amp;gt; {
        if( x in index){
            return index[x];
        } else {
            var b = x *2;
            index[x] = b;
            return b;
        }
     }
}

var c = mem_a();
// Does calculation
console.log(c(5));
&amp;gt; 10
// Uses memoization
console.log(c(5));
&amp;gt; 10

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It stores result in memory so we should avoid it for function which are called frequently and also we should not index the errors thrown by the calculation if any.&lt;/p&gt;

&lt;p&gt;So to sum it up this is like wasting memory to reduce execution time of an expensive function.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Currying in JavaScript</title>
      <dc:creator> Bishnu Prasad Chowdhury</dc:creator>
      <pubDate>Thu, 11 Mar 2021 16:44:08 +0000</pubDate>
      <link>https://dev.to/bishnucit/currying-in-javascript-3npe</link>
      <guid>https://dev.to/bishnucit/currying-in-javascript-3npe</guid>
      <description>&lt;p&gt;In JavaScript a function with multiple arguments can be broken into a series of interconnected functions where each function returns a new arrow function that accepts a single argument. It goes on until the arguments are finished so a 3 argument function can have a function inside it returning another function taking up one argument each.&lt;/p&gt;

&lt;p&gt;Currying can be achieved with only those function that accepts multiple argument else not.&lt;/p&gt;

&lt;p&gt;A normal function would look like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum(x,y,z){
  return x+y+z;
}

sum(2,3,4)
&amp;gt;9

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's see how the same function would look like while curried&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum(x){
  return (y) =&amp;gt; {
    return (z) =&amp;gt; {
      return x+y+z;
    }
  }
}

sum(2)(3)(4)
&amp;gt; 9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last function that is returned has access to the outer function scope (closure feature) hence it can easily get the values of x and y and return the sum with z. &lt;/p&gt;

&lt;p&gt;Currying is useful in those scenarios where a function accepts multiple arguments where one or two argument are constant on every function call i.e their value stays the same. In that scenario we can use currying and reduce the effort of passing the constant argument in each function call.&lt;/p&gt;

&lt;p&gt;Suppose a function fx is having a formula x + y + z + c where c is constant say €25 for every function call but the other values changes everytime. Then we can write like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fx(x){
  return (y) =&amp;gt; {
    return (z) =&amp;gt; {
      return (c) =&amp;gt; {
        return x+y+z+c;
      }
    }
  }
}

const simplifyFX = fx(25);
simplifyFX(1)(2)(3)
&amp;gt;31

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use lodash library to curry function on the fly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = function (x,y,z){
  return x+y*z;
}

var curryMe = _.curry(a);
var net =  curryMe(5);

net(2,3);
&amp;gt;11
net(2)(2);
&amp;gt;9
net(2,3,4)
&amp;gt;11
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also achieve currying using bind on the above function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var net2 = a.bind(this, 5)
net2(2,3)
&amp;gt;11
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Constructor functions in JavaScript</title>
      <dc:creator> Bishnu Prasad Chowdhury</dc:creator>
      <pubDate>Sat, 06 Mar 2021 08:07:05 +0000</pubDate>
      <link>https://dev.to/bishnucit/constructor-functions-in-javascript-o24</link>
      <guid>https://dev.to/bishnucit/constructor-functions-in-javascript-o24</guid>
      <description>&lt;p&gt;Constructor functions are regular functions in JavaScript that can be used to create objects that have similar methods and properties.&lt;/p&gt;

&lt;p&gt;In a normal scenario, we would use object literals to create objects with certain characteristics like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var Image = {
    URL: "https://www.domain.com/image.jpg",
    height: 400,
    width: 400
};

Image.URL
&amp;gt;"https://www.domain.com/image.jpg"
Image.height
&amp;gt;400
Image.width
&amp;gt;400

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But when we have to deal with multiple objects with similar characteristics, then we can use the constructor function to declare a characteristic and then reuse it by creating objects of that function again and again.&lt;/p&gt;

&lt;p&gt;So in a constructor function, we declare the properties with "this" keyword and we create an object of the function with the "new" keyword like below and we have to pass the arguments to the function which is to be used during object creation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var Image = function(URL, width, height) {
    this.URL = URL; 
    this.width = width;
    this.height = height;
};

var img1 = new Image ("https://www.domain.com/img1", 400, 600);
var img2 = new Image ("https://www.domain.com/img2", 500, 500);

img1.URL
&amp;gt;"https://www.domain.com/img1"

img1.height
&amp;gt;600

img2. width
&amp;gt;500

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike normal functions, constructor functions are named with their first letter in uppercase. &lt;/p&gt;

&lt;p&gt;We can also set a new property of the constructor function separately using the prototype object where all the objects created from the constructor function can make use of it. &lt;br&gt;
Suppose we want to add a new property opacity to the Image function without disturbing the main code, we should do like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var Image = function(URL, width, height) {
    this.URL = URL; 
    this.width = width;
    this.height = height;
};

var img1 = new Image ("https://www.domain.com/img1", 500, 500);
var img2 = new Image ("https://www.domain.com/img2", 600, 600);

Image.prototype.opacity = 0;

img1.opacity
&amp;gt;0

img2.opacity
&amp;gt;0

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For normal functions, the default return type is of Undefined type, whereas the default return type of Constructor functions is "this" parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function normalFunc() {}
var cf = function ConstructorFunction() {}

var a = new cf ();
var b = normalFunc();

a
&amp;gt;ConstructorFunction {}

b
&amp;gt;undefined

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Closures in JavaScript</title>
      <dc:creator> Bishnu Prasad Chowdhury</dc:creator>
      <pubDate>Wed, 03 Mar 2021 16:28:43 +0000</pubDate>
      <link>https://dev.to/bishnucit/closures-in-javascript-3cij</link>
      <guid>https://dev.to/bishnucit/closures-in-javascript-3cij</guid>
      <description>&lt;p&gt;In JavaScript we can pack a function inside another function. When one function A is inside another function B then function A is called outer function while function A is called inner function and this phenomenon is called a closure.&lt;br&gt;
In closure the inner function can have access to the outer function scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function B() {
  let x = 7;
  function A() {
     console.log(x);
  }
  return A;
}

var c = B();
c();

&amp;gt;7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Closures are used to make private function or variables that has limited access. The outer function of a closure cannot access the inner function scope so it can be used to hide data.&lt;/p&gt;

&lt;p&gt;Apart from this use closure do have drawbacks where as long as closure is running memory cannot be garbage collected resulting to memory leaks. This can be avoided by making them null force fully after the use.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Event bubbling in JavaScript</title>
      <dc:creator> Bishnu Prasad Chowdhury</dc:creator>
      <pubDate>Thu, 25 Feb 2021 12:38:08 +0000</pubDate>
      <link>https://dev.to/bishnucit/event-bubbling-in-javascript-2j6k</link>
      <guid>https://dev.to/bishnucit/event-bubbling-in-javascript-2j6k</guid>
      <description>&lt;p&gt;Event bubbling is an event propagation type which is observed in DOM API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
  &amp;lt;p&amp;gt;
    &amp;lt;img&amp;gt; &amp;lt;/img&amp;gt;
  &amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example suppose all 3 elements have registered to handle an event, then as per bubbling rule the event will be handled from inner to outer that is img element will handle the event first then p and last will be div element.&lt;/p&gt;

&lt;p&gt;For general use event bubbling is convenient but some rare cases we need to handle outer event first than inner so in those situation we need to avoid event bubbling.&lt;/p&gt;

&lt;p&gt;This is achieved by making useCapture true to the event handler function where by default it's passed as false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;btn.addEventListener('click',action item(),true);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR&lt;/p&gt;

&lt;p&gt;To stop bubbling we can use either&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// stops immediately
event.stopImmediatePropagation()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//stops after running current event
event.stopPropagation()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Reusing object properties with call() method in JavaScript</title>
      <dc:creator> Bishnu Prasad Chowdhury</dc:creator>
      <pubDate>Mon, 15 Feb 2021 07:39:49 +0000</pubDate>
      <link>https://dev.to/bishnucit/reusing-object-properties-with-call-method-in-javascript-643</link>
      <guid>https://dev.to/bishnucit/reusing-object-properties-with-call-method-in-javascript-643</guid>
      <description>&lt;p&gt;Suppose we have an object with properties that we want to re-use for a certain new object. &lt;/p&gt;

&lt;p&gt;JavaScript provides a call method that we can use to achieve this objective.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Sample object
var emp = {
   salary_per_day: 10,
   monthlySalary: function(){
   return this.salary_per_day * 30;
   }
};

//call the function 
emp.monthlySalary();
&amp;gt; 300

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use the call method here on the new object to use the method of the existing object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var emp = {
   salary_per_day: 10,
   monthlySalary: function(){
   return this.salary_per_day * 30;
   }
};


var emp2 = {salary_per_day: 50};

emp.monthlySalary.call(emp2);
&amp;gt; 1500

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like this. call function can be utilized to reuse the property of another function.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Difference between Object.freeze and Object.seal in JavaScript</title>
      <dc:creator> Bishnu Prasad Chowdhury</dc:creator>
      <pubDate>Tue, 09 Feb 2021 13:59:57 +0000</pubDate>
      <link>https://dev.to/bishnucit/difference-between-object-freeze-and-object-seal-in-javascript-3gon</link>
      <guid>https://dev.to/bishnucit/difference-between-object-freeze-and-object-seal-in-javascript-3gon</guid>
      <description>&lt;p&gt;There are times when we want our objects to retain their original properties such that the property should be not changeable or editable.&lt;/p&gt;

&lt;p&gt;We can change or add property of an object like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = { 
    color: 'red'
};
//Object.freeze(a)

a.value = 10;

&amp;gt;a
[object Object] {
  color: 'red',
  value: 10
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can add Object.freeze(a); to the above code to prevent value property to be added to the a object.&lt;/p&gt;

&lt;p&gt;Also Object.freeze(a) does not allow the property of an object to be changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = {id: 1};
a.id=5;
console.log(a);
&amp;gt; 5


var b = { cost: 100;};
Object.freeze(a);
b.cost= 200
console.log(a);
&amp;gt; 100

&amp;gt;delete b.cost
false

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We cannot perform delete operation with an object which is freeze where on using delete it will return false.&lt;/p&gt;

&lt;p&gt;Object.seal allows to change an existing property unlike Object.freeze&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = { id: 6};
Object.seal(a);
a.cost=500;
a.id=10;

&amp;gt;a
[object Object]{
   id:10
}

&amp;gt;delete a.id
false

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now if we want to allow delete to work on the object we can use Object.preventExtensions() instead of Object.seal()&lt;/p&gt;

&lt;p&gt;So the below is the observation on using prevent extension&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = {id: 6};
Object.preventExtensions(a);
a.id=9;
a.cost=100;

&amp;gt;a
[object Object]{
   id: 9
}

&amp;gt; delete a.id
true

&amp;gt; a
[object Object] {...}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is another way for freezing a property instead of the whole object with Object.freeze.&lt;/p&gt;

&lt;p&gt;Suppose we want to freeze id of an object a only rather than the whole object, we can use it like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = { id : 5, cost : 100, quantity: 50};

Object.defineProperty( a, 'id', { writable: false, value:5});

a.id = 10;
a.quantity=10
a.cost= 60

&amp;gt;a
[object Object]{
   id:5,
   cost:60,
   quantity: 10
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Difference between undefined and null in JavaScript</title>
      <dc:creator> Bishnu Prasad Chowdhury</dc:creator>
      <pubDate>Thu, 04 Feb 2021 14:32:12 +0000</pubDate>
      <link>https://dev.to/bishnucit/difference-between-undefined-and-null-in-javascript-3422</link>
      <guid>https://dev.to/bishnucit/difference-between-undefined-and-null-in-javascript-3422</guid>
      <description>&lt;p&gt;Undefined is set by javascript automatically when a value of a variable is defined but value is not set or not declared at all. &lt;/p&gt;

&lt;p&gt;Null is an empty object which is set by the programmer to reset a value of a variable.&lt;/p&gt;

&lt;p&gt;If undefined is passed to a function as argument it does not over write the default value where as null does. If function parameter is not passed it is undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = {};
&amp;gt; undefined

let b = function (a=true){console.log(a);}

b(null);
&amp;gt; null

b(undefined);
&amp;gt; true

function c(a,b){ console.log(a,b);}
c(null, 5);
&amp;gt; 5

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Null is an empty object type and undefined is of type undefined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;typeof(undefined);
&amp;gt; undefined

typeof(null);
&amp;gt; object

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Null means nothing and undefined means not defined hence both can mean false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;null == undefined;
&amp;gt; true

null === undefined;
&amp;gt; false

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Undefined is not valid in JSON but null is valid.&lt;/p&gt;

&lt;p&gt;Adding undefined with a number result to NaN where as null gives the same number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 5 + undefined
a
&amp;gt; NaN

let b = 6 + null
b
&amp;gt; 6

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally both null and undefined usage should be avoided. We do have optional chaining that can help avoid using null.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Type Conversion in JavaScript wrt boolean, undefined, and null</title>
      <dc:creator> Bishnu Prasad Chowdhury</dc:creator>
      <pubDate>Sat, 30 Jan 2021 04:12:19 +0000</pubDate>
      <link>https://dev.to/bishnucit/type-conversion-in-javascript-wrt-boolean-undefined-and-null-22ci</link>
      <guid>https://dev.to/bishnucit/type-conversion-in-javascript-wrt-boolean-undefined-and-null-22ci</guid>
      <description>&lt;p&gt;We can convert to a string type using the String class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 5;
typeof(a);
&amp;gt; "number"
typeof(String(a));
&amp;gt; "string"

let b = true;
String(b);
&amp;gt; "true"

let c = undefined;
String(c);
&amp;gt; "undefined"

let d = null;
String(d);
&amp;gt; "null"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, to convert to a number we can use the Number class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
let a = "5" ;
typeof(a);
&amp;gt; "string"
typeof(Number(a));
&amp;gt; "number"

let b = true;
Number(b);
&amp;gt; 1

let c = undefined;
Number(c);
&amp;gt; NaN

let d = null;
Number(d);
&amp;gt; 0

//when an operator is there it auto converts
// to number unless its a + operator


let e = "6" / "2";  
e
&amp;gt;3

let e = "6" + "2";
e
&amp;gt;62

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Converting to Boolean uses Boolean class like String and Number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
let a = "5" ;
typeof(a);
&amp;gt; "string"
typeof(Boolean(a));
&amp;gt; "boolean"

let b = true;
Boolean(b);
&amp;gt; true

let c = undefined;
Boolean(c);
&amp;gt; false

let d = null;
Boolean(d);
&amp;gt; false

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Javascript prototype and prototypal inheritance</title>
      <dc:creator> Bishnu Prasad Chowdhury</dc:creator>
      <pubDate>Thu, 21 Jan 2021 15:52:46 +0000</pubDate>
      <link>https://dev.to/bishnucit/javascript-prototype-and-prototypal-inheritance-feh</link>
      <guid>https://dev.to/bishnucit/javascript-prototype-and-prototypal-inheritance-feh</guid>
      <description>&lt;p&gt;When an object/array/ function is created, the javascript engine adds access to hidden properties and method under _ _ proto _ _ object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function b (){
   console.log("function");
};
let a = {
  id : 1,
  name : "jack",
  showScore: function (){
      console.log(this.id + " :" + this.name);
    }
}
a._ _ proto _ _
Object.prototype

a._ _ proto _ _ . _ _ proto _ _
null

b. _ _ proto _ _
Function.prototype

b. _ _ proto _ _ . _ _ proto _ _
Object.prototype

b. _ _ proto _ _ . _ _ proto _ _ . _ _ proto _ _
null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hence we can say everything in javascript is an object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = {
  name: "mavy",
  age: 16,
  place: "AU"
  showName: function (){
     console.log(this.name);
   };
};

let b = {
  name: "redis"
};


b. _ _ proto _ _ = a
// Avoid such code

b.name
"redis"

b.age
16

b.showName()
"redis"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here b is inheriting from a using prototyping inheritance.&lt;/p&gt;

&lt;p&gt;We can implement new logic to existing methods of prototype and everytime it will be called it will load that function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function.prototype.test =  function (){ console.log(true); }

function a() {};

a.test()
true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like this, all functions can now have access to the function through prototype.test property.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Javascript : types of functions</title>
      <dc:creator> Bishnu Prasad Chowdhury</dc:creator>
      <pubDate>Wed, 20 Jan 2021 18:28:09 +0000</pubDate>
      <link>https://dev.to/bishnucit/javascript-types-of-functions-754</link>
      <guid>https://dev.to/bishnucit/javascript-types-of-functions-754</guid>
      <description>&lt;p&gt;&lt;strong&gt;Function statements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;// Allows hoisting&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function test(){
   console.log("function statements");
};
test();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Function expressions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;// Doesnot allow hoisting&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = function (){
   console.log(function expression);
};
a();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Named function expressions&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = function test(){
   console.log("named function expressions");
};
a();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Function declaration&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function test() {
   console.log("function declaration");
};
test();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Anonymous function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = (){
};
x();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;First class function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;// Ability to pass as argument and return functions as value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = function test(abc){
  console.log(abc);
  return function a(){
  };
};
x(function () {};);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Arrow functions&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = () =&amp;gt; console.log("Arrow functions");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Generator functions&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function* square(a){
   for(let x=a;; x*=a){ 
      yield x;
      }
};

for(let z of square(2)){
    console.log(z);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;new Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;// Allows to convert string to function useful to handle server return codes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = new Function ('a','b', 'return a+b');
console.log( sum(3,6));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Javascript not defined vs undefined</title>
      <dc:creator> Bishnu Prasad Chowdhury</dc:creator>
      <pubDate>Tue, 19 Jan 2021 16:41:05 +0000</pubDate>
      <link>https://dev.to/bishnucit/javascript-not-defined-vs-undefined-1d8e</link>
      <guid>https://dev.to/bishnucit/javascript-not-defined-vs-undefined-1d8e</guid>
      <description>&lt;p&gt;undefined example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a) 
// memory reserved for a
var a = 5;
console.log(a)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
undefined&lt;br&gt;
5&lt;/p&gt;

&lt;p&gt;Not defined example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(b)
// No memory is reserved
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;Uncaught reference error: b is not defined&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
