5 quick JavaScript hacks

5 quick JavaScript hacks

1.   Bool condition check

This is one of the basic oversights that each software engineer would have done at least once in their coding professions. Let’s take a look at the code below.

let x = 0;

 let y = 0;

if (x === 0) {

  y = 1;

 } else {

  y = 2;

 }

 
  

The above code looks right, right? Also, it is, however there is something more we can do with it. Now take a look at the code.

let x = 0;

let y = 0;

if (!x) {

  y = 1;

} else {

  y = 2;

}

Does this look any better? So for the reality, we don't have to expressly check for 0 here as it’s a falsy esteem. We can just check, if not x then go inside the condition.

2.   Ternary Condition

Each developer has utilized if condition at any rate once in their lifetime yet at places where there isn't much logic inside the condition we can just supplant it with ternary operators. Why? To spare a couple of lines ;). Let’s take a similar case from above and actualize it utilizing ternary operator.

let x = 0;

let y = 0;

y = !x ? 1 : 2;

So here in the above code rather than 5 lines of code we have basically actualized a similar logic in a single line. Let’s investigate another bit of code.

if (whatever condition) {

  ----------------

  some logic here

  ----------------

  return x ? true : false;

}

There is nothing incorrect in the above code yet you could basically maintain a distance from the ternary operator here. how? take a look at the code beneath.


if (whatever condition) {

  ----------------

  some logic here

  ----------------

  return x; // if you are sure x is boolean

  return !!x; // if x can be anything other than boolean

}

So from above we can see that we don't generally require ternary operators and make an effort not to ever utilize nested ternary operators.

3.   Equal check

Let’s take a look at the code specifically to comprehend it better.

if (false == 0) {

  return true;

} else {

  return false;

}

if (false === 0) {

  return true;

} else {

  return false;

}

Both are returning true, right?? Mmm not right :p, 1st condition will return true and second will return false. The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

4.   Add and concatenate

Addition and concatenation will rely upon the data types as we don't explicitly characterize variables with datatypes in JavaScript.

var a = 10;

var b = '20';

var c = 30;
return a + b; // returns 1020

return b + c; // returns 2030

return a + c; // returns 40

So we can find in the above case, on the off chance that we attempt to add a string and an integer it will restore a concatenated string however in the case of two integers it returns sum of both.

5.   Variable as a key

const a = 1;

let b = '';

if(a) {

  b = 'first Name';

 } else {

  b = 'Last Name';

}

const someObject = {

  b : 'Anser'

};

return someObject;

Here in the above code, we needed to return object as { 'first Name' : 'Anser' } yet what it returns is { b: 'Anser' } since b is utilized as key. So to take care of the above issue we utilize square brackets []. See the code below:

if(a) {

  b = 'first Name';

} else {

  b = 'Last Name';

}

const someObject = {

  [b] : 'Anser'

};

return someObject;
 
  

Now it returns what we needed it to return { 'first Name' : 'Anser' }.

Hopefully you have learnt something from this article. Comments and feedback always make the writer happy ;)

要查看或添加评论,请登录

Muhammad Anser的更多文章

  • How to make an offline first React App

    How to make an offline first React App

    I did a POC on offline first React app and decided to share my POC with the developer community. Suppose we are to…

    7 条评论
  • Convert Redux to Hooks

    Convert Redux to Hooks

    In this article, I will try to explain how you can use Redux with React Hooks. React Redux gave support for Hooks in…

    7 条评论
  • Convert traditional GraphQL Query to?Hooks

    Convert traditional GraphQL Query to?Hooks

    If you don’t know about React hooks and how to use them with the GraphQL API, have a look at this article first…

  • Why I turned down job offers from some big companies

    Why I turned down job offers from some big companies

    When there are more job seekers looking for work than jobs available (think: the Great Recession) it’s rare for…

    2 条评论
  • Using React Hooks with the GraphQL API

    Using React Hooks with the GraphQL API

    What are Hooks actually? Hooks are the functions that lets you get to 'state' without using a class component. Rather…

    1 条评论
  • ReactJS or AngularJS, which to choose

    ReactJS or AngularJS, which to choose

    In the previous couple of years, sites have developed into complex web applications, and what used to be place that is…

  • Things I learned so far as a Software Engineer

    Things I learned so far as a Software Engineer

    I'd love to share five of the most imperative things I've adapted up to this point; I believe that all developers…

  • You can’t hide from Facebook

    You can’t hide from Facebook

    Facebook has since quite a while ago enabled you to download a chronicle of the considerable number of information the…

  • Front-end vs Back-end. Which and Why?

    Front-end vs Back-end. Which and Why?

    I have been working as a developer for more than 5 years now, having almost 3 years experience in backend development…

  • Being toxic in workplace

    Being toxic in workplace

    I have been working as a software engineer for more than 5 years now. I have worked in typical software industry as…

    4 条评论

社区洞察

其他会员也浏览了