<?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: Jehad Hossain</title>
    <description>The latest articles on DEV Community by Jehad Hossain (@jehad_hossain).</description>
    <link>https://dev.to/jehad_hossain</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%2F787527%2F07c38839-3138-474c-9919-d6c8579200ff.png</url>
      <title>DEV Community: Jehad Hossain</title>
      <link>https://dev.to/jehad_hossain</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jehad_hossain"/>
    <language>en</language>
    <item>
      <title>JavaScript Common String Methods</title>
      <dc:creator>Jehad Hossain</dc:creator>
      <pubDate>Wed, 20 Apr 2022 13:41:11 +0000</pubDate>
      <link>https://dev.to/jehad_hossain/javascript-common-string-methods-54ok</link>
      <guid>https://dev.to/jehad_hossain/javascript-common-string-methods-54ok</guid>
      <description>&lt;p&gt;In this article I’m going to discuss with you about the string methods in JavaScript charAt, concat, includes, endsWith, indexOf, lastIndexOf, replace, slice, split, startsWith, substring, toLowercase, toUppercase, trim, trimStart, and trimEnd.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;charAt(): It returns the text at the index which is given as the input.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = "hello world"
console.log(`text at the index 4 is ${text.charAt(4)}`);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;concat(): It combines all the given strings and returns a new string.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const vowels = "aeiou"
const consonents = "bcdfghjklmnpqrstvwxyz";
const word0 = "Hello world.
const word1 = " Hi world.";
const word2 = " Bye world.";
const alphabets = vowels.concat(consonents);
const sentence = word0.concat(word1, word2);
console.log(alphabets);
console.log(sentence);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;includes(): It tries to find the element which is given as input if it finds the element on the string it returns true if not then false.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const vowels = "aeiou

console.log(vowels.includes("u"));// true

console.log(vowels.includes("z"));// false

const consonents = "bcdfghjklmnpqrstvwxyz"

console.log(consonents.includes("h"))// true

console.log(consonents.includes("a"))// false"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;endsWith(): It checks the last character of a string. If it matches then it returns true if not false. You can give the last character or the last word to find or match.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = "hello world"

console.log(text.endsWith("d")); // true

console.log(text.endsWith("world")); // true

console.log(text.endsWith("l"));// false;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;indexOf(): It returns the index number of the given input if the input does not match then it returns -1. It takes two parameters the first one is the element and the second one is the starting index number which is optional.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const alphabets = "abcdefghijklmnpqrstuvwxyza"

console.log(alphabets.indexOf("i")); // output: 8

console.log(alphabets.indexOf("a", 1)); // output: 25

console.log(alphabets.indexOf("o")); // output: -1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;lastIndexOf(): It searches the string for the given input from backward of the string. It returns the index number of the matched element. If no element is matched it returns -1.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const alphabets = "abcdefghijklmnpqrstuvwxyza"

console.log(alphabets.lastIndexOf("i")); // output: 8

console.log(alphabets.lastIndexOf("a")); // output: 25

console.log(alphabets.lastIndexOf("o")); // output: -1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;replace(): It replaces the given input from the string and returns a new string. It takes two parameters first one is which word to replace and the second parameter is what will be set on the replaced position if the second parameter is not given it returns undefined on the replaced position. You can use regex as the first input.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = "The quick brown Bear jumps over the lazy dog."

const regex = /Bear/;

console.log(text.replace("Bear", "goat"));

console.log(text.replace(regex, "lion"));;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;slice(): It returns a piece of text from a string. It takes two parameters one is the starting index and the second is the ending index. If you just give the first index then it will copy all the elements of a string after that index and return a new string. If you give a two-parameter then it will copy from the start index to the end index and return a new string.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = "The quick brown fox jumps over the lazy dog."

console.log(text.slice(10)); // output: brown fox jumps over the lazy dog.

console.log(text.slice(10, 20)); // output: brown fox

// if you use negative index then it will count from the end of string

console.log(text.slice(-9, -1)); // output: lazy dog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;split(): It divides the string by following the pattern which is given as input. Then it puts the divided parts into an array and returns the array.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = "The quick brown fox jumps over the lazy dog."

const words = "ab, eb, ib, ob, ub";

console.log(text.split(" "));
console.log(words.split("b"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;startsWith(): It checks the string if it has started with the word or letter which has been given as the input to this method. If the word matches then it returns true if not then false.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = "The quick brown fox jumps over the lazy dog."

console.log(text.startsWith("T")); // output: true

console.log(text.startsWith("The")); // output: true

console.log(text.startsWith("A")); // output: false

console.log(text.startsWith("quick")); // output: false;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;substring(): It returns the part of a string. It takes two parameters first one is the starting index and the second one is the ending index. If you don’t give the second parameter then it will return everything after the starting index as a new string.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = "The quick brown fox jumps over the lazy dog."

console.log(text.substring(10)); // output: brown fox jumps over the lazy dog.

console.log(text.substring(4, 15)); // output: quick brown

// if you give greater value on the first parameter then it will automatically swap the parameters

console.log(text.substring(15, 4)); // output: quick brown;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;toLowerCase(): Makes all the words of the calling string into lower case. Then returns them as a string.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = "The Quick Brown Fox Jumps Over The Lazy Dog."

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;toUpperCase(): Makes all the words of the calling string into upper case. Then returns them as a string.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = "The quick brown fox jumps over the lazy dog."

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;trim(): It removes all the white spaces from starting and ending of the calling string. And returns a new string.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = "      Hello world     "

console.log(text.trim()); // output: Hello world;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;trimStart(): It removes the white spaces from the beginning of the calling string. And returns a new string.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = "      Hello world     "
console.log(text.trimStart());;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;trimEnd(): It removes the white spaces from the end of the calling string. And returns a new string.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const text = "      Hello world     "
console.log(text.trimEnd());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>useCallback, useMemo, useRef, and useReducer hook</title>
      <dc:creator>Jehad Hossain</dc:creator>
      <pubDate>Tue, 11 Jan 2022 14:52:53 +0000</pubDate>
      <link>https://dev.to/jehad_hossain/usecallback-usememo-useref-and-usereducer-hook-dl</link>
      <guid>https://dev.to/jehad_hossain/usecallback-usememo-useref-and-usereducer-hook-dl</guid>
      <description>&lt;h2&gt;
  
  
  In this article, I’m going to discuss the use of useCallback, useMemo, useRef, and useReducer hook.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;useCallback hook:&lt;/strong&gt; It memorizes a callback function. So new instances of that function are not created. And it will only forget it when the dependency value is changed.&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, { useCallback, useState } from "react";
import "./App.css";
import Title from "./components/Title/Title";

function App() {
 const [increase1, setIncrease1] = useState(0);
 const [increase5, setIncrease5] = useState(0);

 const handleIncrease1 = useCallback(() =&amp;gt; {
   console.log("inside 1");
   setIncrease1(increase1 + 1);
 }, [increase1]);

 const handleIncrease5 = useCallback(() =&amp;gt; {
   console.log("inside 5");
   setIncrease5(increase5 + 5);
 }, [increase5]);

 return (
   &amp;lt;div className="App"&amp;gt;
     &amp;lt;Title /&amp;gt;
     {/* increase value by 1 */}
     &amp;lt;h4&amp;gt;Increase value by 1&amp;lt;/h4&amp;gt;
     &amp;lt;p&amp;gt;{increase1}&amp;lt;/p&amp;gt;
     &amp;lt;button onClick={handleIncrease1}&amp;gt; Increase by 1&amp;lt;/button&amp;gt;

     {/* increase value by 5 */}
     &amp;lt;h4&amp;gt;Increase value by 5&amp;lt;/h4&amp;gt;
     &amp;lt;p&amp;gt;{increase5}&amp;lt;/p&amp;gt;
     &amp;lt;button onClick={handleIncrease5}&amp;gt; Increase by 5&amp;lt;/button&amp;gt;
   &amp;lt;/div&amp;gt;
 );
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;UseMemo hooks:&lt;/strong&gt; It’s a little different from useCallback hook. useCallback memorizes the whole function but it memorizes only the return value of a function. And it forgets that when the dependency value is changed.&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, { useCallback, useMemo, useState } from "react";
import "./App.css";
import Title from "./components/Title/Title";

function App() {
 const [increase1, setIncrease1] = useState(0);
 const [increase5, setIncrease5] = useState(0);

 const handleIncrease1 = useCallback(() =&amp;gt; {
   console.log("inside 1");
   setIncrease1(increase1 + 1);
 }, [increase1]);

 const handleIncrease5 = useCallback(() =&amp;gt; {
   console.log("inside 5");
   setIncrease5(increase5 + 5);
 }, [increase5]);

 const isEvenOrOdd = useMemo(() =&amp;gt; {
   let i = 0;
   while (i &amp;lt; 1000000000) i++;
   return increase1 % 2 === 0;
 }, [increase1]);

 return (
   &amp;lt;div className="App"&amp;gt;
     &amp;lt;Title /&amp;gt;
     {/* increase value by 1 */}
     &amp;lt;h4&amp;gt;Increase value by 1&amp;lt;/h4&amp;gt;
     &amp;lt;p&amp;gt;{increase1}&amp;lt;/p&amp;gt;
     &amp;lt;p&amp;gt;{isEvenOrOdd ? "Even" : "Odd"}&amp;lt;/p&amp;gt;
     &amp;lt;button onClick={handleIncrease1}&amp;gt; Increase by 1&amp;lt;/button&amp;gt;

     {/* increase value by 5 */}
     &amp;lt;h4&amp;gt;Increase value by 5&amp;lt;/h4&amp;gt;
     &amp;lt;p&amp;gt;{increase5}&amp;lt;/p&amp;gt;
     &amp;lt;button onClick={handleIncrease5}&amp;gt; Increase by 5&amp;lt;/button&amp;gt;
   &amp;lt;/div&amp;gt;
 );
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;useRef:&lt;/strong&gt; If we want to get a element or its value then we use document.getElementById or document.getElementsByClassName etc. You can use them on react but it is not a good practice. To solve this react give us useRef hook. You just have to say which node element you want to reference. Then it will return the referenced node in the referenced variable.current&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, { useEffect, useRef } from "react";

const Form = () =&amp;gt; {
  const inputRef = useRef();

  useEffect(() =&amp;gt; {
    console.log(inputRef.current.value);
  }, []);
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;input ref={inputRef} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If you have to use ref to a child component then you have to use forward ref to pass ref from parent to child component&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Parent component where the ref is located&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;import React, { useEffect, useRef } from "react";
import "./App.css";
import Input from "./components/Input";

function App() {
 const inputRef = useRef();

 useEffect(() =&amp;gt; {
   inputRef.current.focus();
 });
 return (
   &amp;lt;div className="App"&amp;gt;
     &amp;lt;Input placeholder="Enter your name" ref={inputRef} type="text" /&amp;gt;
   &amp;lt;/div&amp;gt;
 );
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Child component where I want to use the ref&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;import React from "react";

const Input = ({ type, placeholder }, ref) =&amp;gt; {
 return &amp;lt;input ref={ref} type={type} placeholder={placeholder} /&amp;gt;;
};

const forwardedInput = React.forwardRef(Input);

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Here’s a little trick of using useRef. When you want to use a function which is scoped inside the useEffect hook.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

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

const Clock = () =&amp;gt; {
 const [date, setDate] = useState(new Date());
 const intervalRef = useRef();

 const tick = () =&amp;gt; {
   setDate(new Date());
 };

 useEffect(() =&amp;gt; {
   intervalRef.current = setInterval(tick, 1000);

   // do the cleanup stop the timer
   return () =&amp;gt; {
     clearInterval(intervalRef.current);
   };
 }, []);
 return (
   &amp;lt;div&amp;gt;
     &amp;lt;p&amp;gt;Time: {date.toLocaleTimeString()}&amp;lt;/p&amp;gt;
     &amp;lt;p&amp;gt;
       &amp;lt;button onClick={() =&amp;gt; clearInterval(intervalRef.current)}&amp;gt;Stop&amp;lt;/button&amp;gt;
     &amp;lt;/p&amp;gt;
   &amp;lt;/div&amp;gt;
 );
};

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;useReducer:&lt;/strong&gt; useReducer is like the Array.prototype.reduce method in vanilla JS. The difference is that reduce method takes a reducer function and initialValue but useReducer takes the reducer function and initialState as the second parameter. Reduce method returns a single value but useReducer returns a tuple [newState, dispatch]&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, { useReducer } from "react";
import "./App.css";

const initialState = 0;
const reducer = (state, action) =&amp;gt; {
 switch (action) {
   case "increment":
     return state + 1;
   case "decrement":
     return state - 1;
   default:
     return state;
 }
};
function App() {
 const [count, dispatch] = useReducer(reducer, initialState);
 return (
   &amp;lt;div className="App"&amp;gt;
     &amp;lt;p&amp;gt;{count}&amp;lt;/p&amp;gt;
     &amp;lt;button onClick={() =&amp;gt; dispatch("increment")}&amp;gt;Increase&amp;lt;/button&amp;gt;
     &amp;lt;button onClick={() =&amp;gt; dispatch("decrement")}&amp;gt;Decrease&amp;lt;/button&amp;gt;
   &amp;lt;/div&amp;gt;
 );
}

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can use useReducer in a complex situation where you have different as for example counters and they have a different functions to update their value.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

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

const initialState = {
 counter: 0,
 counter2: 0,
};
const reducer = (state, action) =&amp;gt; {
 /* you have to merge object immutably to do that use the spread operator */
 switch (action.type) {
   case "increment":
     return { ...state, counter: state.counter + action.value };
   case "decrement":
     return { ...state, counter: state.counter - action.value };
   case "increment2":
     return { ...state, counter2: state.counter2 + action.value };
   case "decrement2":
     return { ...state, counter2: state.counter2 - action.value };
   default:
     return state;
 }
};

/* you have to merge state immutably */
function Counter() {
 const [count, dispatch] = useReducer(reducer, initialState);
 return (
   &amp;lt;React.Fragment&amp;gt;
     {/* counter 1 */}
     &amp;lt;div&amp;gt;
       &amp;lt;p&amp;gt;Counter1: {count.counter}&amp;lt;/p&amp;gt;
       &amp;lt;button
         onClick={() =&amp;gt;
           dispatch({
             type: "increment",
             value: 1,
           })
         }
       &amp;gt;
         Increment by 1
       &amp;lt;/button&amp;gt;

       &amp;lt;button
         onClick={() =&amp;gt;
           dispatch({
             type: "decrement",
             value: 1,
           })
         }
       &amp;gt;
         Decrement by 1
       &amp;lt;/button&amp;gt;
     &amp;lt;/div&amp;gt;

     {/* counter 2 */}
     &amp;lt;div&amp;gt;
       &amp;lt;p&amp;gt;Counter2: {count.counter2}&amp;lt;/p&amp;gt;
       &amp;lt;button
         onClick={() =&amp;gt;
           dispatch({
             type: "increment2",
             value: 1,
           })
         }
       &amp;gt;
         Increment2 by 1
       &amp;lt;/button&amp;gt;

       &amp;lt;button
         onClick={() =&amp;gt;
           dispatch({
             type: "decrement2",
             value: 1,
           })
         }
       &amp;gt;
         Decrement2 by 1
       &amp;lt;/button&amp;gt;
     &amp;lt;/div&amp;gt;
   &amp;lt;/React.Fragment&amp;gt;
 );
}

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

&lt;/div&gt;



</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
