<?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>Forem: David Igheose</title>
    <description>The latest articles on Forem by David Igheose (@davidigheose).</description>
    <link>https://forem.com/davidigheose</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%2F471316%2Fcbf1a406-efed-4b4c-9a25-ad1f8b10e457.png</url>
      <title>Forem: David Igheose</title>
      <link>https://forem.com/davidigheose</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/davidigheose"/>
    <language>en</language>
    <item>
      <title>How to Catch Errors During Rendering in React</title>
      <dc:creator>David Igheose</dc:creator>
      <pubDate>Fri, 12 Aug 2022 13:23:00 +0000</pubDate>
      <link>https://forem.com/davidigheose/how-to-catch-errors-during-rendering-in-react-39j</link>
      <guid>https://forem.com/davidigheose/how-to-catch-errors-during-rendering-in-react-39j</guid>
      <description>&lt;p&gt;When building a react application errors are bound to happen in our applications, whether they’re server-related errors or edge cases. As such, many methods have been developed to prevent these errors from interfering with the user and developer experience. In React, one such method is the use of error boundaries.&lt;/p&gt;

&lt;h1&gt;
  
  
  What are Error Boundaries?
&lt;/h1&gt;

&lt;p&gt;Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.&lt;a href="https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html"&gt;source: official react js website&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To solve this problem for React users, React 16 introduces a new concept of an “error boundary”. There are limits to the built-in error boundary in React 16v but in this case, we can use a package called &lt;a href="https://www.npmjs.com/package/react-error-boundary"&gt;react-error-boundary&lt;/a&gt;, Let's build a small project to explain the concept behind error boundary.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Note: We will be covering a few advanced concepts of React. So having intermediate knowledge of React is advisable.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let Us Go Through The React Built-in Error boundary.
&lt;/h2&gt;

&lt;p&gt;The built-in error boundary can't catch errors for  Event handlers, Asynchronous code, Server-side rendering, and Errors thrown in the error boundary itself.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;What are event handlers? &lt;br&gt;
An event handler is a callback routine that operates asynchronously once an event takes place.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is asynchronous code? &lt;br&gt;
Asynchronous programming is a technique that enables your program to start a potentially long-running task and still be able to be responsive to other events while that task runs, rather than having to wait until that task has finished. Once that task has finished, your program is presented with the result. &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing"&gt;Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is server-side rendering?&lt;br&gt;
Server-side rendering refers to an application’s ability to display the web page on the server rather than rendering it in the browser. When a website’s JavaScript is rendered on the website’s server, a fully rendered page is sent to the client and the client’s JavaScript bundle engages and enables the Single Page Application framework to operate. &lt;a href="https://www.heavy.ai/technical-glossary/server-side-rendering"&gt;Source&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;while &lt;strong&gt;&lt;code&gt;Error thrown&lt;/code&gt;&lt;/strong&gt; lets you indicate that something unexpected happened and the normal flow of execution can't continue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's code
&lt;/h2&gt;

&lt;p&gt;Facing errors in your react applications,  we can see error logs like this in development.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gE_T-MFR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7e79ccltjfmk0m08oty.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gE_T-MFR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7e79ccltjfmk0m08oty.PNG" alt="Image description" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The user won't be seeing error logs but white screens just like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kfM4u2PD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rznq2et8odopjp7khiz3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kfM4u2PD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rznq2et8odopjp7khiz3.jpg" alt="Image description" width="800" height="289"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So how can we handle these types of errors in your code by using a method called Error boundaries in react?&lt;/p&gt;

&lt;h3&gt;
  
  
  The built-in way
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a file called  builtInErrorBoundary.js in your src folder copy and paste this code inside it.
&lt;/li&gt;
&lt;/ul&gt;

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

export class ErrorBoundary extends Component {
  constructor(props) {
    super(props);
    this.state = {
      hasError: false
    };
  }

  static getDerivedStateFromError(error) {
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    console.log("log errors Info", error, errorInfo);
  }

  render() {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return &amp;lt;h1&amp;gt;Something went wrong.&amp;lt;/h1&amp;gt;;
    }

    return this.props.children;
  }
}

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To catch errors during rendering we just need to wrap this with our main component pages  just like this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";
import { ErrorBoundary } from "./builtInErrorBoundary";
import ProfileCard from "./component/profile";
import "./styles.css";

export default function App() {
  const [ageState, setAgeState] = useState(1);

  const incrementYourAge = () =&amp;gt; setAgeState(ageState + 1);

  return (
    &amp;lt;ErrorBoundary&amp;gt;
      &amp;lt;div className="App"&amp;gt;
        &amp;lt;div className="container"&amp;gt;
           &amp;lt;ProfileCard ageState={"10"} /&amp;gt;
          {/* try this to see the Something went wrong screen
          &amp;lt;ProfileCard ageState={{}} /&amp;gt; 
          */}
          &amp;lt;button onClick={incrementYourAge}&amp;gt;
            &amp;lt;h3&amp;gt;Click here to state your age&amp;lt;/h3&amp;gt;
          &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/ErrorBoundary&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Check out the live code:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You can check out the below code sandbox for complete code and the file structure may not be the best.
&lt;a href="https://codesandbox.io/embed/how-to-catch-errors-during-rendering-in-react-gv0mzx?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark"&gt;Code sandbox link&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What the user can see is your custom fallback UI something like this when an error accords, you can make the design better like a 404 page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZzDlWCSc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d97ynfop6vx7s48qnm5s.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZzDlWCSc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d97ynfop6vx7s48qnm5s.jpg" alt="Image description" width="800" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Best Way To Go About Error Boundaries For Your Next Big Project.
&lt;/h2&gt;

&lt;p&gt;I  explained that the built-in error boundary can't catch errors for  Event handlers, Asynchronous code, Server-side rendering, and Errors thrown in the error boundary itself.&lt;/p&gt;

&lt;p&gt;So what can we do about this problem, there are two ways to solve this problem &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Building your error boundaries boilerplate for all the solutions you need which will take time &lt;/li&gt;
&lt;li&gt;By using an open-source npm package called &lt;a href="https://www.npmjs.com/package/react-error-boundary"&gt;react-error-boundary&lt;/a&gt; which solves all our problems by just installing the package (Thanks to the creators of this package). &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This component provides a simple and reusable wrapper that you can use to wrap around your components. Any rendering errors in your components hierarchy can then be gracefully handled. &lt;/p&gt;

&lt;h2&gt;
  
  
  For Example :
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {ErrorBoundary} from 'react-error-boundary'

function ErrorFallback({error, resetErrorBoundary}) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Something went wrong:&amp;lt;/p&amp;gt;
      &amp;lt;span&amp;gt;{error.message}&amp;lt;/pre&amp;gt;
      &amp;lt;button onClick={resetErrorBoundary}&amp;gt;Try again&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default function DashboardPage () {
  function errorHandler(error, errorInfo) {
    console.error("log", error, errorInfo);
  }

 return (
 &amp;lt;ErrorBoundary
    FallbackComponent={ErrorFallback}
    onError={errorHandler}&amp;gt;
    onReset={() =&amp;gt; {
      // reset the state of your app so the error doesn't happen again
    }}
  &amp;gt;
    &amp;lt;div&amp;gt;
      &amp;lt;h3&amp;gt;Welcome back David&amp;lt;/h3&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/ErrorBoundary&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What's next?
&lt;/h3&gt;

&lt;p&gt;Now you know the basics of Error Boundaries. You know what it is, and why it can be useful, even how to apply it in your react application. But nothing will convince you more than some hands-on experience. This package makes it easy and faster, how to handle Error Boundaries &lt;a href="https://www.npmjs.com/package/react-error-boundary"&gt;Learn more here&lt;/a&gt;. ✌️&lt;/p&gt;

&lt;h3&gt;
  
  
  Thank you for reading
&lt;/h3&gt;

&lt;p&gt;Feel free to subscribe to my email newsletter and &lt;a href="https://linktr.ee/davidigheose"&gt;Connect with me&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Getting Started With Babel - Transpiling Javascript</title>
      <dc:creator>David Igheose</dc:creator>
      <pubDate>Wed, 10 Aug 2022 14:17:00 +0000</pubDate>
      <link>https://forem.com/davidigheose/getting-started-with-babel-transpiling-javascript-3d03</link>
      <guid>https://forem.com/davidigheose/getting-started-with-babel-transpiling-javascript-3d03</guid>
      <description>&lt;p&gt;Babel is a generic multi-purpose compiler for JavaScript. Using Babel you can create the next generation of JavaScript, as well as the next generation of JavaScript tooling.&lt;/p&gt;

&lt;p&gt;JavaScript as a language is constantly evolving, with new specs and proposals coming out with new features all the time. Using Babel will allow you to use many of these features years before they are available everywhere.&lt;/p&gt;

&lt;p&gt;Babel does this by compiling down JavaScript code written with the latest standards into a version that will work everywhere today. This process is known as source-to-source compiling, also known as transpiring. &lt;a href="https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/user-handbook.md"&gt;Source:  Jamie Kyle&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Babel?
&lt;/h3&gt;

&lt;p&gt;Babel is a javascript compiler  which means javascript to javascript compiling, &lt;br&gt;
babel is divided into different steps :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Input source code&lt;/li&gt;
&lt;li&gt; Transformed AST&lt;/li&gt;
&lt;li&gt; Generating Original AST
&lt;/li&gt;
&lt;li&gt; Output source code &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;this is done by three different  packages &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/parser &lt;/li&gt;
&lt;li&gt; &lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/traverse&lt;/li&gt;
&lt;li&gt; &lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/generator,  they are all inside babel-core which provides a simple API because using three different packages just to compile your program isn’t idle so we have roughed them in one package making it easy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Talking about babel-parser
&lt;/h3&gt;

&lt;p&gt;Babel parser is divided into  different phases :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lexical analysis :
Transform the input source code into a list of tokens
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age = 10;

/* var age = 7;

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

&lt;/div&gt;



&lt;p&gt;during this phase, it reports errors about invalid literal or invites chapters for example if we have a non-traumatic comment or string if there is a chapter that doesn’t make sense, it report’s the errors.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Syntax analysis :&lt;br&gt;
It gets the list of tokens generated during the Lexical analysis and builds the abstract syntax tree.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Semantic analysis :&lt;br&gt;
It checks the AST respects all the static ECMAScript rules called early errors and also it reports errors about invalid variables, using a scope tracker.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Features of Babel
&lt;/h3&gt;

&lt;p&gt;Here are the main things Babel can do for you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transform syntax&lt;/li&gt;
&lt;li&gt; Polyfill features that are missing in your target environment (through a third-party polyfill such as core-js)&lt;/li&gt;
&lt;li&gt; Source code transformations&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Presets
&lt;/h3&gt;

&lt;p&gt;Babel presets can act as a sharable set of Babel plugins and/or config options.&lt;br&gt;
We've assembled a few presets for common environments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/preset-env for compiling ES2015+ syntax&lt;/li&gt;
&lt;li&gt; &lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/preset-typescript for TypeScript&lt;/li&gt;
&lt;li&gt;  &lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/preset-react for React&lt;/li&gt;
&lt;li&gt;  &lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/preset-flow for Flow&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Configuration files
&lt;/h3&gt;

&lt;p&gt;Babel has two parallel config file formats, which can be used together, or independently.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Project-wide configuration&lt;/li&gt;
&lt;li&gt;&lt;p&gt;babel.config.* files, with the following extensions: .json, .js, .cjs, .mj&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;File-relative configuration&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;.babelrc.* files, with the following extensions: .json, .js, .cjs, .mjs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;.babelrc file, with no extension.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;package.json files, with a "babel" key.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What's next?
&lt;/h3&gt;

&lt;p&gt;Now you know the basics of how babel transpiling Javascript. You know what it is, and why it can be useful. But nothing will convince you more than some hands-on experience.  For more understanding, you can check out  :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The  official babel website :  &lt;a href="https://babeljs.io/"&gt;Link here ✌️&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;Babel User Handbook Written by Jamie Kyle  :  &lt;a href="https://github.com/jamiebuilds/babel-handbook"&gt;Link here ✌️&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Thank you for reading
&lt;/h3&gt;

&lt;p&gt;Feel free to subscribe to my email newsletter and &lt;a href="https://linktr.ee/davidigheose"&gt;Connect with me&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Understanding the ` This` Keyword in Javascript</title>
      <dc:creator>David Igheose</dc:creator>
      <pubDate>Wed, 10 Aug 2022 14:12:00 +0000</pubDate>
      <link>https://forem.com/davidigheose/understanding-the-this-keyword-in-javascript-2dnl</link>
      <guid>https://forem.com/davidigheose/understanding-the-this-keyword-in-javascript-2dnl</guid>
      <description>&lt;h3&gt;
  
  
  What is the &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;  keyword?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;  keyword references to the object that is executing the current function, it’s a javascript built-in keyword that can be used in different forms for example :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if that function is part of an object, we call the function and method&lt;/li&gt;
&lt;li&gt;if that function is a method in an object, the  &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;  keyword references that object itself&lt;/li&gt;
&lt;li&gt;if that function is a regular function which means it's not part of an object, the &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;  keyword refers to the global object which is the window object&lt;/li&gt;
&lt;li&gt;In strict mode, the &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;  keyword is undefined&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/"&gt;Source:  stackoverflow&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;  keyword is not a variable, you can't change the value of the &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;  keyword. It is a keyword that refers to an object. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The question is, what is the value of  &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; almost everyone who has ever written javascript has at some point run into an error.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;  keyword refers to the object that is executing the current function. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is 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;const person = {
  name: 'David Igheose',
  type: 'frontend developer',

  career: function () {
  console.log(`My name is ${this.name} am a ${this.type}`)
  }
}

person.career()

// Output:
// My name is David Igheose am a frontend developer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why the &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;  keyword?
&lt;/h3&gt;

&lt;p&gt;We know the  &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;  keyword is not a variable, it refers to the current object,  so why do we have to use the  &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; keyword to refer to an item in an object, when we can just call the item itself, here is a code example that explains why we use the  &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; keyword.&lt;/p&gt;

&lt;p&gt;So we have two object data called person one and person two :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// person one 
const person1 = {
  name: 'John don',
  type: 'Blockchain developer',
}

// person two 
const person2 = {
  name: 'David Igheose',
  type: 'frontend developer',
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in the above objects, we are adding a function to the objects which looks 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;// persona one
const person1 = {
  name: 'John don',
  type: 'Blockchain developer',

  career: function () {
  console.log(`My name is ${person1.name} am a ${person1.type}`)
  }
}
person1.career()
// Output:
// My name is John don am a Blockchain developer


// person two
const person2 = {
  name: 'David Igheose',
  type: 'frontend developer',

  career: function () {
  console.log(`My name is ${person2.name} am a ${person2.type}`)
  }
}

person2.career()

// Output:
// My name is David Igheose am a frontend developer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;from the code example, we can see that the &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;  keyword is not used, we are just using the object name to refer to the item inside it, 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;// person one
career: function () {
  console.log(`My name is ${person1.name} am a ${person1.type}`)
 }

// person two
career: function () {
  console.log(`My name is ${person2.name} am a ${person2.type}`)
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the problem with this method is that using the object name to refer to an item can cause a code error in the sense that if one decides to refer to the person1 object inside person2 object, 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;// persona one
const person1 = {
  name: 'John don',
  type: 'Blockchain developer',

  career: function () {
  console.log(`My name is ${person1.name} am a ${person1.type}`)
  }
}

person1.career()

// Output:
// My name is John don am a Blockchain developer

// person two
const person2 = {
  name: 'David Igheose',
  type: 'frontend developer',

  career: function () {
  console.log(`My name is ${person1.name} am a ${person1.type}`)
  }
}

person2.career()

// Output:
// My name is John don am a Blockchain developer

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

&lt;/div&gt;



&lt;p&gt;the output of the two object method calls we be the same, this is why when referring to the object that is executing the current function we need to use the keyword  &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  “This” keyword  in  strict mode
&lt;/h3&gt;

&lt;p&gt;Outside any object,  &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt;  in strict mode is always undefined. Notice I mentioned strict mode. If strict mode is disabled (the default state if you don't explicitly add 'use strict' on top of your file ), you are in the so-called sloppy mode, and &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; - unless some specific cases mentioned here below - has the value of the global object. Which means a window in a browser context. &lt;a href="https://flaviocopes.com/javascript-this/"&gt;Source: flaviocopes&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  “This” keyword inside an Arrow function
&lt;/h3&gt;

&lt;p&gt;When using the  &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; keyword inside an arrow function, the arrow function blocks the  &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; keyword from referring which means rather than printing the value, the referred item becomes undefined. 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;// person object
const person = {
  name: 'David Igheose',
  type: 'frontend developer',

  career: ()  =&amp;gt; {
  console.log(`My name is ${this.name} am a ${this.type}`)
  }
}

person.career()

// Output:
// My name is undefined am a undefined

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

&lt;/div&gt;



&lt;p&gt;for functions, we have three types for them which are :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function Declaration - automatically bound to the object.&lt;/li&gt;
&lt;li&gt;Function Expression - automatically bound to the object.&lt;/li&gt;
&lt;li&gt;Arrow Function  -  it's lexically bound.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regular functions created using function declarations or expressions are constructible and callable. Since regular functions are constructible, they can be called using the &lt;code&gt;new&lt;/code&gt; keyword.&lt;br&gt;
However, the arrow functions are only callable and not constructible, i.e arrow functions can never be used as constructor functions. &lt;a href="https://betterprogramming.pub/difference-between-regular-functions-and-arrow-functions-f65639aba256#:~:text=Regular%20functions%20created%20using%20function,be%20used%20as%20constructor%20functions"&gt;Source: Ashutosh Verma&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  The call() or apply()
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;call&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;apply&lt;/code&gt;&lt;/strong&gt; are very similar—they invoke a function with a specified &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; context, and optional arguments. The only difference between &lt;strong&gt;&lt;code&gt;call&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;apply&lt;/code&gt;&lt;/strong&gt; is that &lt;strong&gt;&lt;code&gt;call&lt;/code&gt;&lt;/strong&gt; requires the arguments to be passed in one by one, and &lt;strong&gt;&lt;code&gt;apply&lt;/code&gt;&lt;/strong&gt; takes the arguments as an array.&lt;br&gt;
&lt;a href="https://www.taniarascia.com/this-bind-call-apply-javascript/"&gt;Source: Tania Rascia&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we'll create an object, and create a function that references &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; but has no &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// person object
const person = {
  name: 'David Igheose',
  type: 'frontend developer',
}


const career = function(props) {
  console.log(`My name is ${this.name} am a ${this.type} and a ${props}`)
}

career.call(person, "technical writer")

// Output:
// My name is David Igheose am a frontend developer and a technical writer

career.apply(person, ["technical writer"])
// Output:
// My name is David Igheose am a frontend developer and a technical writer

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

&lt;/div&gt;



&lt;p&gt;we can see the difference between the call() and apply() method, the  call() method takes &lt;br&gt;
the arguments separately while the apply() method takes the arguments as an array.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's next?
&lt;/h3&gt;

&lt;p&gt;Now you know the basics of how the &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; keyword works. You know what it is, and why it can be useful. But nothing will convince you more than some hands-on experience.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Thank you for reading
&lt;/h3&gt;

&lt;p&gt;Feel free to subscribe to my email newsletter and &lt;a href="https://linktr.ee/davidigheose"&gt;Connect with me&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
