Some tips JavaScript & React

Kawsar Ahmed
6 min readNov 5, 2020

01. How to works React State Hook

function useState(initiaValue){   var myData = initiaValue;   function state(){       return myData;   }   function setState(newValue){       myData = newValue;   }   return[state, setState];}const [data, setData] = useState(0);console.log(data());  //retrun 0setData(101);console.log(data()); // retrun 101
  • At first, we are creating a function this name is useState().
  • Then we create a global variable like the as myData.
  • Then we create a function like name state & setState, inside in useState() function.
  • state function returns a global variable myData.
  • setState function takes one parameter & store parameter in global variable myData.
  • Then parent function useState() return state & setState in a array.
  • Now if we call a useState() function then we destructuring the array in my custom name.
  • Then we console this my custom name like data.
  • This data is rename to a state function.
  • We see our expected result.
  • If we want to set data, then we call a setData function.

02. How we can get Truthy and Falsy values

// 0 is falsey values
const num = 0;
if(num){ console.log("Truthy Values");}else { console.log("Falsey Values");}
  • 0 number is falsey values.
  • Without 0 & -0 remaining all numbers Truthy values
// false keyword is falsey values
const num = false
if(num){console.log("Truthy Values");}else {console.log("Falsey Values");
  • The false keyword is falsy values, but if we use false keyword in string then it will be truthy values.
// empty string is falsey values
const num = ""
if(num){console.log("Truthy Values");}else {console.log("Falsey Values");
  • An empty string is falsy values, but if we put space in empty string then it will be truthy values.
// undefined / Nan/ Null is faley values
const num = undefined
if(num){console.log("Truthy Values");}else {console.log("Falsey Values");
  • undefined is falsey values.
  • Null / Nan is falsey values.
  • Remaining all Truthy values.

03. Null Vs Undefined

  • Undefined means it is declared but the value is not assigned.
  • If we set any variable values undefined then we get an undefined message.
  • We declare a variable but we are not assigned this value, then we will see an undefined message.
  • if we create a function but not return any value, if we call this function in the console then we will see an undefined message.
  • Return keyword use in function but not set any values for return then we will see an undefined message.
  • Function created and works in a function use a parameter but we do not pass parameter then we get an undefined message.
  • We have created an object and assigned many key pair values.But we call an unknown key then we see an undefined message.
  • Null means values do not exist.
  • We can get Null values when we are works in values but we are set values is Null, then we will Null values.

04. Double equal vs Thripple equal

const num1 = 101;const num2 = "101";if(num1 == num2){    console.log("Condition is true");}else {    console.log("Condition is false");}
  • If we set the variable in num1 values in the number value and set the num2 values is string values.
  • Now if we check the condition num1 == num2 then we can see the condition is true.
const num1 = 101;const num2 = "101";if(num1 === num2){console.log("Condition is true");}else {console.log("Condition is false");}
  • Now if we check the condition num1 === num2 then we can see the condition is false.
  • Cause == means condition check only values. It may be a number or string.
  • But when we check === then it will check values & values data Type.
  • For this reason, the condition goes to else condition.

05. Apply map, filter, find on an array of objects

Array.Map()

const names = [{   name: "Kawsar",   age: 24,   job: "Web Development"},{   name: "Ahmed",   age: 24,   job: "React developer"},{   name: "Shamim",   age: 24,   job: "Biman pilot"},];const result = names.map( person => console.log(person.name))// Kawsar// Ahmed// Shamim
  • We can use a map instead of a loof.
  • Array.map() return a array.
  • If we want to get an object then we render an array map and pass an arrow function in the map.
  • When the map renders continuously then we can get an object by using the arrow function.
  • We can easily use map in our projects.

Array.filter()

const names = [{   name: "Kawsar",   age: 24,   job: "Web Development"},{   name: "Ahmed",   age: 25,   job: "React developer"},{   name: "Shamim",   age: 26,   job: "Biman pilot"},];const result = names.filter( person => person.age > 24)console.log(result); [
{ name: 'Ahmed', age: 25, job: 'React developer' },
{ name: 'Shamim', age: 26, job: 'Biman pilot' }
]
  • If we need some sp[ecific value then we can use an array. filter
  • Array.filter return an array.
  • We can take easily apply array.filter in our projects

Array.find()

const names = [{   name: "Kawsar",   age: 24,   job: "Web Development"},{   name: "Ahmed",   age: 25,   job: "React developer"},{   name: "Shamim",   age: 26,   job: "Biman pilot"},];const result = names.find( person => person.age > 24)console.log(result);  // { name: 'Ahmed', age: 25, job: 'React developer' }
  • Array.find() we are uses in when we need an exact value.
  • Array.find returns an exact value.

06.Problem Solving — find the largest element of an array

const numbers = [10, 13, 9, 18, 16, 23, 39, 48, 56, 79, 89, 99, 64, 81, 74];  var max = 0;  for (let i = 0; i < numbers.length; i++) {     const element = numbers[i];     if(element > max){     max = element;   }}console.log(max); // return 99
  • At first, we thought my first initial max value = 0;
  • Somehow we can check all values compare to my initial max values then we can get max values.
  • So we can check by using a loop.
  • Using for loop we can get a single value then we are compared to may max values.
  • If single values grater than my initial max values then we will reset to my initial value and set my initial value = single value.
  • When the loop stop we will see our expected result.

07. Problem Solving — Sum of all numbers in an array

const numbers = [10, 13, 9, 18, 16, 23, 39, 48, 56, 79, 89, 99, 64, 81, 74];var sum = 0;for (let i = 0; i < numbers.length; i++) {sum += numbers[i];}console.log(sum); // return 718
  • At first, we declare sum = 0.
  • then we are using for loop render our array.
  • When rendering an ay we can get single value.
  • If I can get single value then we add our sum values
  • When stooping the loop we can see our result.

08. Problem Solving — Remove duplicate item from an array

const numbers = [10, 9, 18, 16, 23, 16, 48, 9, 79, 23, 99, 10, 81, 10];var unique = [];for (let i = 0; i < numbers.length; i++) {   const element = numbers[i];   const index = unique.indexOf(element)   if(index == -1){      unique.push(element);   }}console.log(unique);
// [
// 10, 9, 18, 16, 23,// 48, 79, 99, 81// ]

09. Problem Solving — Count the number of words in a string

const sentence = "I love Web programming";const length = sentence.length; // length 22const count = sentence.split(" ");console.log(count.length); // count word 4// [ 'I', 'love', 'Web', 'programming' ]
  • It is a one way. We can use another way.
  • But I can shortly this way.
  • Ar first, we can check sentence length. This is optional.
  • then we can separate our sentence by using split method.
  • We are know split method return a array. So we can get count array of length.
  • Array.length is our word count.

10.Problem Solving — Reverse a string

const sentence = "I love Web programming";const reverse = sentence.split("").reverse().join('');console.log(reverse); // gnimmargorp beW evol I
  • We can one problem solved many ways.
  • Here I use away.
  • This way at first, I can split the sentence.
  • We know split returns an array. If we can get an array then we will change the array text position by using array.reverse().
  • then we can get all text wrapped. So we can join this one to one by using array.join().
  • In the end,we see our expected result.

--

--