<?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: Emran H Khan</title>
    <description>The latest articles on DEV Community by Emran H Khan (@emranhkhan).</description>
    <link>https://dev.to/emranhkhan</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F626696%2Fd68380d6-f98c-485c-9d56-8e2d237a5b15.jpeg</url>
      <title>DEV Community: Emran H Khan</title>
      <link>https://dev.to/emranhkhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emranhkhan"/>
    <language>en</language>
    <item>
      <title>Titbits of JavaScript</title>
      <dc:creator>Emran H Khan</dc:creator>
      <pubDate>Sat, 08 May 2021 02:48:49 +0000</pubDate>
      <link>https://dev.to/emranhkhan/titbits-of-javascript-b3g</link>
      <guid>https://dev.to/emranhkhan/titbits-of-javascript-b3g</guid>
      <description>&lt;p&gt;&lt;strong&gt;Truthy &amp;amp; Falsy:&lt;/strong&gt; In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. &lt;br&gt;
A falsy value is a value that is considered false when encountered in a Boolean context.&lt;br&gt;
All values are truthy unless they are defined as falsy (i.e., except for false, 0, -0, 0n, "", null, undefined, and NaN).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Type coercion vs Type conversion:&lt;/strong&gt; Type coercion "implicitly" converts one data type to another. But in case of type conversion, it can either be implicit or explicit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const value1 = '5';
const value2 = 9;
let sum = value1 + value2;

console.log(sum); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, 9 which is a number value has been coerced into string by JavaScript. And it's done implicitly. We have no hand in that. The result of this operation is 59, which not a number but a string. So, if wanted to get the result what we expected then we need to convert the "value1" into a number first and then do the operation. Like so, &lt;code&gt;sum = Number(value1) + value2;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;== vs ===:&lt;/strong&gt; When we are using (==) in JS, we are actually testing for loose equality. Another interesting thing happens here, which is &lt;strong&gt;&lt;em&gt;type coercion&lt;/em&gt;&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;77 === '77'
// false (Number v. String)

false == 0
// true

0 == ""
// true

"" == false
// true 

null == null
// true

undefined == undefined
// true

null == undefined
// true

NaN == null
// false

NaN == undefined
// false

NaN == NaN
// false
//NaN is not equivalent to anything. Cool fact: it’s not even itself!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using triple equals === in JavaScript, we are testing for strict equality. This means both the &lt;strong&gt;&lt;em&gt;type&lt;/em&gt;&lt;/strong&gt; and the &lt;strong&gt;&lt;em&gt;value&lt;/em&gt;&lt;/strong&gt; we are comparing have to be the same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5 === 5
// true

5 === 5
// true

77 === '77'
// false (Number v. String)

'cat' === 'dog'
// false (Both are Strings, but have different values)

false === 0
// false (Different type and different value)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Scope:&lt;/strong&gt; It determines the accessibility of the variables.&lt;br&gt;
In JavaScript there are three kinds of scope:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Global Scope: Variables declared Globally (outside any function) have Global Scope. Global variables can be accessed from anywhere in a JavaScript program.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var carName = "Volvo";

// code here can use carName

function myFunction() {
  // code here can also use carName
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Function/Local Scope: Variables declared Locally (inside a function) have Function Scope. Local variables can only be accessed from inside the function where they are declared.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// code here can NOT use carName

function myFunction() {
  var carName = "Volvo";
  // code here CAN use carName
}

// code here can NOT use carName
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Block Scope: Variables declared with the &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; keyword can have Block Scope. Variables declared inside a block {} cannot be accessed from outside the block.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  let x = 2;
}
// x can NOT be used here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Closure:&lt;/strong&gt; Closure means that an inner function always has access to the variables and parameters of its outer function, even after the outer function has returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function OuterFunction() {
  var outerVariable = 100;

  function InnerFunction() {
    alert(outerVariable);
    }
    return InnerFunction;
}

var innerFunc = OuterFunction();
innerFunc(); // 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, &lt;code&gt;return InnerFunction;&lt;/code&gt; returns InnerFunction from OuterFunction when you call OuterFunction(). A variable innerFunc reference the InnerFunction() only, not the OuterFunction(). So now, when you call innerFunc(), it can still access outerVariable which is declared in OuterFunction(). This is called Closure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Window:&lt;/strong&gt; The window object is supported by all browsers. It represents the browser's window.&lt;/p&gt;

&lt;p&gt;All global JavaScript objects, functions, and variables automatically become members of the window object.&lt;/p&gt;

&lt;p&gt;Global variables are properties of the window object.&lt;/p&gt;

&lt;p&gt;Global functions are methods of the window object.&lt;/p&gt;

&lt;p&gt;Even the document object (of the HTML DOM) is a property of the window object: &lt;code&gt;window.document.getElementById("header");&lt;/code&gt; is same as &lt;code&gt;document.getElementById("header");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;this keyword:&lt;/strong&gt; In a method, &lt;code&gt;this&lt;/code&gt; refers to the owner 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 person = {
  firstName: "John",
  lastName : "Doe",
  id     : 5566,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};
person.fullName(); // John Doe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alone, &lt;code&gt;this&lt;/code&gt; refers to the global object. &lt;code&gt;var x = this // [object Window]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In a function, &lt;code&gt;this&lt;/code&gt; refers to the global 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 myFunction() {
  return this;
}

myFuction() // [object Window]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In an event, this refers to the element that received the event.&lt;br&gt;
&lt;code&gt;&amp;lt;button onclick="this.style.display='none'"&amp;gt;Click to Remove Me!&amp;lt;/button&amp;gt;&lt;/code&gt;. Here &lt;code&gt;this&lt;/code&gt; will refer to the button element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;setTimeOut:&lt;/strong&gt; The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds.&lt;br&gt;
&lt;code&gt;setTimeout(function(){ alert("Hello"); }, 3000);&lt;/code&gt;.&lt;br&gt;
Display an alert box after 3 seconds (3000 milliseconds).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;setInterval:&lt;/strong&gt; The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).&lt;br&gt;
&lt;code&gt;setInterval(function(){ alert("Hello"); }, 3000);&lt;/code&gt;.&lt;br&gt;
Alert "Hello" every 3 seconds (3000 milliseconds).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;call method:&lt;/strong&gt; The &lt;code&gt;call()&lt;/code&gt; method is a predefined JavaScript method. It can be used to invoke &lt;code&gt;(call)&lt;/code&gt; a method with an owner object as an argument (parameter).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var person = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}
var person1 = {
  firstName:"John",
  lastName: "Doe"
}

person.fullName.call(person1);  // Will return "John Doe"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example calls the fullName method of person, using it on person1.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>es6</category>
    </item>
    <item>
      <title>REACT basics</title>
      <dc:creator>Emran H Khan</dc:creator>
      <pubDate>Fri, 07 May 2021 14:46:29 +0000</pubDate>
      <link>https://dev.to/emranhkhan/rect-basics-510l</link>
      <guid>https://dev.to/emranhkhan/rect-basics-510l</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is React:&lt;/strong&gt; React is JavaScript library created by Facebook. It is a tool for building UI components. React doesn't manipulate the browser's DOM directly, instead it creates copy of the DOM and save it in memory. This copied DOM is generally known as the 'VIRTUAL DOM'. React then finds out what changes have been made, and changes only that part in the DOM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Skills to learn React:&lt;/strong&gt;&lt;br&gt;
    1. HTML &amp;amp; CSS&lt;br&gt;
    2. JSX&lt;br&gt;
    3. Fundamental of JavaScript and ES6&lt;br&gt;
    4. Package manager (Node+Npm)&lt;br&gt;
    5. Git and CLI &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Render Function:&lt;/strong&gt; React renders HTML to web page by using a function called ReactDOM.render(). This function takes two arguments, HTML code and HTML element. The purpose of this function is to display the specified HTML code inside the specified element.&lt;/p&gt;

&lt;p&gt;Display a span inside the 'root' element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(&amp;lt;span&amp;gt;Hello World!&amp;lt;/span&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is displayed in the &lt;code&gt;&amp;lt;div id='root'&amp;gt;&lt;/code&gt; element:&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;body&amp;gt;
  &amp;lt;div id='root'&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The HTML code here uses JSX which allows you to write HTML tags inside the JavaScript code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JSX:&lt;/strong&gt; JSX stands for JavaScript XML. It allows us to write HTML in React. JSX converts the HTML into react elemts.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;With JSX:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const newElement: &amp;lt;h1&amp;gt;Learning JSX!&amp;lt;/h1&amp;gt;
ReactDOM.render(newElement, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Without JSX:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const newElement = React.createElement('h1', {}, 'Not using JSX');
ReactDOM.render(newElement, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is clearly seen from the above example, it is far more easier to write JSX which eventually transpile our HTML to JavaScript at runtime.&lt;/p&gt;

&lt;p&gt;Expression can be written in JSX using the curly braces {}.&lt;br&gt;
And to write multiple HTML lines you have to put parentheses around the HTML and wrap everything in a single Top level element.&lt;br&gt;
For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const newElement = (
  &amp;lt;div&amp;gt;
    &amp;lt;h1&amp;gt;I am a header.&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt; I am a paragraph.&amp;lt;/p&amp;gt;
  &amp;lt;/div&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Functional Component:&lt;/strong&gt; A component is an independent, reusable code block which divides the UI into smaller pieces. A functional component is basically a JavaScript/ES6 function that returns a React element(JSX). It needs to exported to be used later in somewhere else.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Welcome = (props) =&amp;gt; {
  return &amp;lt;h1&amp;gt;Hello, {props.name}&amp;lt;/h1&amp;gt;
}

export default Welcome;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And to use it we need to import it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Welcome from './Welcome';

const App = () =&amp;gt; {
  return(
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;Welcome /&amp;gt;
    &amp;lt;div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Props:&lt;/strong&gt; Props is short for properties ant they are used to pass data between React components. React's data flow between components is uni-directional (from parent to child only);&lt;br&gt;
For example, if you want to pass something from app to component you have to pass it like an attribute with suitable name.&lt;br&gt;
Here, I am passing 'name' from the App component to Welcome component. If you need to pass data dynamically just use the curly braces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Welcome from './Welcome';

const App = () =&amp;gt; {
  return(
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;Welcome name="Justin" /&amp;gt;
    &amp;lt;div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, in the Welcome component we will get the data in the 'props'.&lt;br&gt;
And we can use it like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Welcome = (props) =&amp;gt; {
  return &amp;lt;h1&amp;gt;Hello {props.name}!&amp;lt;/h1&amp;gt;
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;State:&lt;/strong&gt; React has another special built-in object called state, which allows components to create and manage their own data. So unlike props, components cannot pass data with state, but they can create and manage it internally.&lt;br&gt;
React components are rendered (with state) based on the data in the state. State holds the initial information. So, when state changes, React gets informed and immediately re-renders the part of the DOM which actually needs to be changed. There is method called 'setState' which triggers the re-rendering process for the updated parts. React gets informed, knows which parts to change, and does it quickly without re-rendering the whole DOM.&lt;br&gt;
In functional components, with the help of React Hooks we can use this 'state'.&lt;br&gt;
We will implement a simple counter using React's useState hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";

function Counter() {
  // Set the initial count state to zero, 0
  const [count, setCount] = useState(0);

  // Create handleIncrement event handler
  const handleIncrement = () =&amp;gt; {
    setCount(prevCount =&amp;gt; prevCount + 1);
  };

  //Create handleDecrement event handler
  const handleDecrement = () =&amp;gt; {
    setCount(prevCount =&amp;gt; prevCount - 1);
  };
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button onClick={handleDecrement}&amp;gt;-&amp;lt;/button&amp;gt;
        &amp;lt;h5&amp;gt;Count is {count}&amp;lt;/h5&amp;gt;
        &amp;lt;button onClick={handleIncrement}&amp;gt;+&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(0)}&amp;gt;Reset&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Counter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And use this component is the App.js like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import Counter from "../Counter";

export default function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;Counter/&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;useEffect:&lt;/strong&gt; A functional React component uses props and/or state to calculate the output. If the functional component makes calculations that don’t target the output value, then these calculations are named side-effects.&lt;/p&gt;

&lt;p&gt;useEffect() hook accepts 2 arguments:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useEffect(callback[, dependencies])&lt;/code&gt;;&lt;/p&gt;

&lt;p&gt;callback is the callback function containing side-effect logic. &lt;code&gt;useEffect()&lt;/code&gt; executes the callback function after React has committed the changes to the screen.&lt;/p&gt;

&lt;p&gt;dependencies is an optional array of dependencies. &lt;code&gt;useEffect()&lt;/code&gt; executes callback only if the dependencies have changed between renderings.&lt;br&gt;
Put your side-effect logic into the callback function, then use the dependencies argument to control when you want the side-effect to run. That’s the sole purpose of &lt;code&gt;useEffect()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React Events:&lt;/strong&gt; Just like HTML, React can perform actions based on user events. Reach has the same events as HTML: click, change, mouserover etc.&lt;br&gt;
React events are written in camelCase sytax: &lt;code&gt;onClick&lt;/code&gt; instead of &lt;code&gt;onclick&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom';

function shoot() {
  alert("Great Shot!");
}

const myelement = (
  &amp;lt;button onClick={shoot}&amp;gt;Take the shot!&amp;lt;/button&amp;gt;
);

ReactDOM.render(myelement, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to pass an argument in the event handler, then you have to wrap the handler into an anonymous arrow function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom';

function shoot(condition) {
  if (condition) {
    alert("Great Shot!");
  }
  alert("Keep going on");
}

const myelement = (
  &amp;lt;button onClick={() =&amp;gt; shoot(true)}&amp;gt;Take the shot!&amp;lt;/button&amp;gt;
);

ReactDOM.render(myelement, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;React CSS:&lt;/strong&gt; To style an element with the inline style attribute, the value must be a JavaScript object. Properties with two name, like &lt;code&gt;background-color&lt;/code&gt;, must be written in camel case syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MyHeader = () =&amp;gt; {
  return (
      &amp;lt;div&amp;gt;
      &amp;lt;h1 style={{color: "white", backgroundColor: 'black'}}&amp;gt;Hello Style!&amp;lt;/h1&amp;gt;
      &amp;lt;p&amp;gt;Add a little style!&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    );
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also create an object with styling information, and refer it in the style attribute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myStyle = {
  color: "white", 
  backgroundColor: 'black'
}

const MyHeader = () =&amp;gt; {
  return (
      &amp;lt;div&amp;gt;
      &amp;lt;h1 style={myStyle}&amp;gt;Hello Style!&amp;lt;/h1&amp;gt;
      &amp;lt;p&amp;gt;Add a little style!&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    );
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>ES6 Features</title>
      <dc:creator>Emran H Khan</dc:creator>
      <pubDate>Thu, 06 May 2021 15:52:41 +0000</pubDate>
      <link>https://dev.to/emranhkhan/es6-features-55on</link>
      <guid>https://dev.to/emranhkhan/es6-features-55on</guid>
      <description>&lt;p&gt;&lt;strong&gt;var:&lt;/strong&gt; Before ES6, variables were declared using var. var declarations are globally scoped or function/locally scoped. When var is declared outside of a function, it is said to be globally scoped. If var is declared within a function, then it is function/locally scoped.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; var greetings = 'hey buddy';
 function myFunc() {
    var hello = 'hello';
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, greetings is globally scoped, because it is declared outside of a function. And hello is locally scoped since it is declared inside a function.&lt;br&gt;
var variables can be re-declared and updated within the same scope and it won't throw an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var greetings = 'hey buddy';
var greetings = 'bye buddy';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we can also do like this-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var greetings = 'hey buddy';
greetings = 'bye buddy';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another important thing is that var variables are hoisted to the top of their scope and initialized with a value of undefined. The main problem with var declaration is that it is able to redefine a variable and if you have declared the same variable in other parts of your code you will get surprising output and this will likely causer bugs in the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let:&lt;/strong&gt; Let is block scoped. So, what is a block? You can think of any code that is bounded by the curly {} braces. So, variable declared using let is only available inside that block. Let me give an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (100 &amp;gt; 10) {
    let greeting = 'hey buddy';
    console.log(greeting); // "hey buddy"
}
console.log(greeting) // greeting is undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, we can see that greeting is undefined outside of the block. Another important thing about let is that it can be updated but not re-declared. And let is hoisted to the top of its scope but never initialized. So, if you try to access let variable before declaration, it will throw an error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;const:&lt;/strong&gt; const and let are basically the same. The main difference between these two is that, const cannot be updated or re-declared.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const greetings = 'hey buddy';
greetings = 'hello buddy'; //error: Assignment to constant variable
const greetings = 'hey buddy';
cosnt greetings = 'hello buddy'; //error: Identifier 'greetings' has already been declared.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, every const variable has to be initialized at the the time of its declaration.&lt;br&gt;
var declarations and hoisting: var declarations are treated as if they were declared at the top of the function (enclosing block) or in the global scope if they were declared outside of a function. So, in a layman's term this is called hoisting. Let me give an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getName(condition){
    if(condition) {
        var name = 'Jalil';
        console.log(name)
    }
    else {
        console.log(name)
    }
    console.log(name)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, if you pass true as an argument in the function you get the output from if and the enclosing block blocks, otherwise, from else and enclosing blocks. But, notice that the name is only declared in the if block. So, what is going on here! What is happening is that variable name is hoisted at the top of the function but within the scope of the function. That's why everywhere inside the function it is available.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Binding:&lt;/strong&gt; Variable is another word can be said as a binding. When we declare/initialize a variable, what we actually do is binding the value to a name. Well it happens inside a scope obviously. So, what is a scope? You can think scope as a specific part of the program.&lt;br&gt;
So, binding happens when actually we declare or initialize a variable using var, let or const in JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Block Level Declarations:&lt;/strong&gt; If variable is declared in a block-level scope using let or const they cannot be accessed outside of the block scope. Block scope can be created in the following places:&amp;nbsp;&lt;br&gt;
&amp;nbsp;1. Within a function (function block).&lt;br&gt;
&amp;nbsp;2. Within a block (bound with curly {} braces)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getMoney(condition) {
    if(condition) {
       let amount = '100$';
       console.log(amount);
    }
    else {
       return null;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this function, amount is only accessible inside the if block. Because it was not hoisted at the top so the name is not accessible inside the else or enclosing blocks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arrow functions:&lt;/strong&gt; These functions were introduce in ES6. These allow us to write shorter function syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;greeting = () {
    return 'hello world';
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It gets even shorter if you it has only one statement. You need not to mention the return keyword and also don't need to put curly braces.&amp;nbsp;&lt;code&gt;greeting = () =&amp;gt; 'hello world';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the function has a single argument then the parentheses can also be omitted&amp;nbsp;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = 'Kudos';
greeting = name =&amp;gt; 'hello' + name;

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

&lt;/div&gt;



&lt;p&gt;With arrow functions the 'this' keyword always represents the object that defined the arrow function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Spread Operator:&lt;/strong&gt; The spread operator '…' is used to expand an iterable or an array. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arrTxt = ['hello', 'Mars', 'goodbye', 'Earth'];
console.log(arrTxt); // ['hello', 'Mars', 'goodbye', 'Earth']
console.log(...arrTxt): // hello Mars goodbye Earth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spread operator can be used to copy an array into another array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr1 = ['hello', 'world'];
const arr2 = [...arr1, 'I', 'love', 'you'];
console.log(arr2) // ['hello', 'world', 'I', 'love', 'you']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spread operator can also be used with object literals. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj1 = {x:1, y:2};
const obj2 = {z:3};
const obj = {...obj1, ...obj2};
console.log(obj) // {x:1, y:2, z:3}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Default function parameters: It allows named parameter to be initialized with default values if no value or 'undefined' is passed. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(a, b = 4) {
    return a + b;
}

add(5, 5) // 9
add (5) // 9
add (5, undefined) //9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Hoisting:&lt;/strong&gt; It is a default behavior of JavaScript of moving all the declarations (not initializations) at the top of the scope before execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function hoisting() {
    a = 10;
    let b = 50;
}
hoisting();

console.log(a); // 10
console.log(b): // ReferenceError: b is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In JavaScript, all undeclared variables are implicitly bound to global context. In other words, they are global variables. This is why when we logged the variable 'a' the output was correct although it is inside the function scope. And since the variable 'b' is block scoped we got a 'ReferenceError'.&lt;/p&gt;

&lt;p&gt;hoisting of var - let's see an 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(name);
var name = 'Katherine Langford'; //undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, we should have got a ReferenceError instead of undefined because, we tried to access the name before declaring it.&lt;br&gt;
But actually the interpreter sees it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name;
console.log(name); // undefined
name = 'Katherine Langford' 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we would have used let instead of var it would show us ReferenceError instead. This happens because in ES6 we can't just have undeclared variables.&lt;br&gt;
const behaves the same way as let when it comes to hoisting.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>es6</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
