Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 222

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T08:57:09+00:00 2024-11-25T08:57:09+00:00

7 Shorthand Optimization Tricks every JavaScript Developer Should Know 😎

  • 62k

Every language has its own quirks and JavaScript, the most used programming language, is no exception. This article will cover a plethora of JavaScript Shorthand Optimization tricks that can help you write better code, and also make sure this is NOT your reaction when you encounter them:

confused-face

1. Multiple string checks

Often you might need to check if a string is equal to one of the multiple values, and can become tiring extremely quickly. Luckily, JavaScript has a built-in method to help you with this.

// Long-hand const isVowel = (letter) => {   if (     letter === "a" ||     letter === "e" ||     letter === "i" ||     letter === "o" ||     letter === "u"   ) {     return true;   }   return false; };  // Short-hand const isVowel = (letter) =>   ["a", "e", "i", "o", "u"].includes(letter); 
Enter fullscreen mode Exit fullscreen mode

2. For-of and For-in loops

For-of and For-in loops are a great way to iterate over an array or object without having to manually keep track of the index of the keys of the object.

For-of

const arr = [1, 2, 3, 4, 5];  // Long-hand for (let i = 0; i < arr.length; i++) {   const element = arr[i];   // ... }  // Short-hand for (const element of arr) {   // ... } 
Enter fullscreen mode Exit fullscreen mode

For-in

const obj = {   a: 1,   b: 2,   c: 3, };  // Long-hand const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) {   const key = keys[i];   const value = obj[key];   // ... }  // Short-hand for (const key in obj) {   const value = obj[key];   // ... } 
Enter fullscreen mode Exit fullscreen mode

3. Falsey checks

If you want to check if a variable is null, undefined, 0, false, NaN, or an empty string, you can use the Logical Not (!) operator to check for all of them at once, without having to write multiple conditions. This makes it easy to check if a variable contains valid data.

// Long-hand const isFalsey = (value) => {   if (     value === null ||     value === undefined ||     value === 0 ||     value === false ||     value === NaN ||     value === ""   ) {     return true;   }   return false; };  // Short-hand const isFalsey = (value) => !value; 
Enter fullscreen mode Exit fullscreen mode

4. Ternary operator

As a JavaScript developer, you must have encountered the ternary operator. It is a great way to write concise if-else statements. However, you can also use it to write concise code and even chain them to check for multiple conditions.

// Long-hand let info; if (value < minValue) {   info = "Value is too small"; } else if (value > maxValue) {   info = "Value is too large"; } else {   info = "Value is in range"; }  // Short-hand const info =   value < minValue     ? "Value is too small"     : value > maxValue ? "Value is too large" : "Value is in range"; 
Enter fullscreen mode Exit fullscreen mode

5. Function calls

With the help of the ternary operator, you can also determine which function to call based on conditions.

IMPORTANT SIDE-NOTE: The call signature of the functions must be the same, else you risk running into an errors

function f1() {   // ... } function f2() {   // ... }  // Long-hand if (condition) {   f1(); } else {   f2(); }  // Short-hand (condition ? f1 : f2)(); 
Enter fullscreen mode Exit fullscreen mode

6. Switch shorthand

Long switch cases can often be optimized by using an object with the keys as the switches and the values as the return values.

const dayNumber = new Date().getDay();  // Long-hand let day; switch (dayNumber) {   case 0:     day = "Sunday";     break;   case 1:     day = "Monday";     break;   case 2:     day = "Tuesday";     break;   case 3:     day = "Wednesday";     break;   case 4:     day = "Thursday";     break;   case 5:     day = "Friday";     break;   case 6:     day = "Saturday"; }  // Short-hand const days = {   0: "Sunday",   1: "Monday",   2: "Tuesday",   3: "Wednesday",   4: "Thursday",   5: "Friday",   6: "Saturday", }; const day = days[dayNumber]; 
Enter fullscreen mode Exit fullscreen mode

7. Fallback values

The || operator can set a fallback value for a variable.

// Long-hand let name; if (user?.name) {   name = user.name; } else {   name = "Anonymous"; }  // Short-hand const name = user?.name || "Anonymous"; 
Enter fullscreen mode Exit fullscreen mode

That's all folks! πŸŽ‰

Finding personal finance too intimidating? Checkout my Instagram to become a Dollar Ninja

Thanks for reading

Need a Top Rated Front-End Development Freelancer to chop away your development woes? Contact me on Upwork

Want to see what I am working on? Check out my Personal Website and GitHub

Want to connect? Reach out to me on LinkedIn

I am a Digital Nomad and occasionally travel. Follow me on Instagram to check out what I am up to.

Follow my blogs for bi-weekly new tidbits on Dev

FAQ

These are a few commonly asked questions I get. So, I hope this FAQ section solves your issues.

  1. I am a beginner, how should I learn Front-End Web Dev?
    Look into the following articles:

    1. Front End Development Roadmap
    2. Front End Project Ideas
  2. Would you mentor me?

    Sorry, I am already under a lot of workload and would not have the time to mentor anyone.

javascriptproductivityprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.