Important Notes on JavaScript

Srabanty
3 min readNov 5, 2020

--

React Hooks:

React.useState()

We use useState() hook in case of changing the state of any page or content. On useState() hook there are two properties. The initial state and the modified state. Modified state is set with the setState property. It works inside a function.

Syntex,

const [state, setState] = useState([])

React.useEffect()

useEffect() hook is used when we want to add any third party API in our code. Using this we load the API. It is also used inside of function.

Syntex,

useEffect(()=>{

fetch(‘…API url………’);

})

ES6:

Truthy and Falsy values

Truthy: The truthy value means the value that is considered as true. For example,

true

“ ” (string with tenth of at least 1)

‘0’

[]

{}

Falsy: The falsy value means the value that is considered as false. For example,

“” (empty string)

NaN

undefined

0

false

Null VS Undefined

Let’s see when will show undefined value.

We set the property value null when we want to show the value is empty.

Double equal (==) VS Triple equal(===)

Double equal(==) in coding only checks the value of two properties and triple equal (===) checks the value as well as the data type.

For example,

map

map function in javaScript works on an array and loops through the array. Return the output based on our condition.

For example,

find

It returns the first element which satisfies the given condition.

For example,

filter

It gives output from an array if our condition is fulfilled. returns an array.

For example,

String reverse

We can use the reverse() function and also manually can make a string reverse.

For example,

let mainString = “Javascript”;

let reverseString = “”;

for (let i=mainString.length — 1; i>=0; i — ) {

reverseString += mainString [i]

}

console.log(reverseString); //output: tpircsavaJ

Using method,

Var newStr = reverseString(“Javascript”);

console.log(newStr); //output: tpircsavaJ

Global variable

Global variable is one kind of variable that is accessible from anywhere in the code. They are declared outside of the function. If declared inside a function then it’s scope is blocked inside the function and not accessible from the outside.

For example,

let num = 10 //global variable

function() {

//….code…..

}

--

--