TIPS & TRICKS OF JAVASCRIPT & REACT

TIPS 1: Easiest way of string to integer conversion.

const value_1 = “1”

const value_2 = “2”

function add(field_1, field_2){

return field_1 + field_2;
}

add(+value_1, +value_2);

TIPS 2: Easiest way of float to inte…


This content originally appeared on DEV Community and was authored by Nipu Chakraborty

TIPS 1: Easiest way of string to integer conversion.

const value_1 = "1" 

const value_2 = "2"

function add(field_1, field_2){

  return field_1 + field_2;
}


add(+value_1, +value_2); 

TIPS 2: Easiest way of float to integer conversion.

const floatToInt = 23.9 | 0;

TIPS 3: Use Global object always should not need localStorage

Note[if data is is static then you should use it. and don’t use any kind secret or confidential data here..]

const user = {

  first_name: "Rowan",

  last_name: "Atkinson"
}

window.user=user

TIPS 3: Don’t use if not necessary ternary (?:)

const DemoComponent = ()=>{

const [show, setShow] = useState(false)

   return (<div>

     {show? <Message/>: ''}

</div>)
}

Right way with (&&)

const DemoComponent = ()=>{
const [show, setShow] = useState(false)

   return (<div>
     {show && <Message/>}
    </div>)
}

TIPS 4: Don’t do it

if (variable1 !== null || variable1 !== undefined || variable1 !== '') {
     let variable2 = variable1;
}

Do this short and simple

const variable2 = variable1  || 'new';

TIPS 5: Don’t do it

Math.floor(4.9) === 4 // true

Do this short and simple

~~4.9 === 4 // true

TIPS 6: Don’t do it

switch (something) {

  case 1:
    doSomething();

  break;
case 2:
    doSomethingElse();
  break;
case 3:
    doSomethingElseAndOver();
  break;
  // And so on...
}

Do this short and simple

const cases = {
  1: doSomething,

  2: doSomethingElse,

  3: doSomethingElseAndOver
};

TIPS 7: Don’t do it

if(x == 1 || x == 5 || x == 7)  {
  console.log('X has some value');
}

Do this short and simple

([1,5,7].indexOf(x) !=- 1) && console.log('X has some value!');

TIPS 8: Don't do it

const param1 =1;
const param2 = 2;
const param3 = 3;
const param4 = 4;


function MyFunc =(param1, param2, param3, param4)=>{
  console.log(param1, param2, param3, param4)
}

MyFunc(param1, param2, param3, param4)

Do this short and simple

const params = {param1: 1, param2: 2, param3: 3, param4: 4}

function MyFunc =({param1, param2, param3, param4})=>{
  console.log(param1, param2, param3, param4)

}

MyFunc(params)

TIPS 9: Don’t do it

function Myfunc(value){
   if(value){
     console.log("you have a value")
   }else{
      throw new Error("You don't have a value")
   }
}

Do this short and simple

NOTE: If you check error first then it’s don’t go else block but if you do first one it will check first value is exist then if not found it will go the else block.

function Myfunc(value){
   return !value ? throw new Error("You don't have a value") : console.log("you have a value")
}


This content originally appeared on DEV Community and was authored by Nipu Chakraborty


Print Share Comment Cite Upload Translate Updates
APA

Nipu Chakraborty | Sciencx (2021-07-24T16:33:03+00:00) TIPS & TRICKS OF JAVASCRIPT & REACT. Retrieved from https://www.scien.cx/2021/07/24/tips-tricks-of-javascript-react/

MLA
" » TIPS & TRICKS OF JAVASCRIPT & REACT." Nipu Chakraborty | Sciencx - Saturday July 24, 2021, https://www.scien.cx/2021/07/24/tips-tricks-of-javascript-react/
HARVARD
Nipu Chakraborty | Sciencx Saturday July 24, 2021 » TIPS & TRICKS OF JAVASCRIPT & REACT., viewed ,<https://www.scien.cx/2021/07/24/tips-tricks-of-javascript-react/>
VANCOUVER
Nipu Chakraborty | Sciencx - » TIPS & TRICKS OF JAVASCRIPT & REACT. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/24/tips-tricks-of-javascript-react/
CHICAGO
" » TIPS & TRICKS OF JAVASCRIPT & REACT." Nipu Chakraborty | Sciencx - Accessed . https://www.scien.cx/2021/07/24/tips-tricks-of-javascript-react/
IEEE
" » TIPS & TRICKS OF JAVASCRIPT & REACT." Nipu Chakraborty | Sciencx [Online]. Available: https://www.scien.cx/2021/07/24/tips-tricks-of-javascript-react/. [Accessed: ]
rf:citation
» TIPS & TRICKS OF JAVASCRIPT & REACT | Nipu Chakraborty | Sciencx | https://www.scien.cx/2021/07/24/tips-tricks-of-javascript-react/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.