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 4100

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

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T08:50:09+00:00 2024-11-26T08:50:09+00:00

Enough JavaScript to get you Started : #18 Optional chaining and nullish coalescing

  • 61k

Optional Chanining ?.

πŸ‘‰ According to MDN The optional chaining operator (?.) permits reading the value of a property located deep within a chain of connected objects without having to expressly validate that each reference in the chain is valid.

πŸ‘‰ To put it more simply , let's take an example where you have to fetch data to client side from the backend, so data come in form of JSON {javaScript object notation} but sometimes due to error or wrong credentials null will be returned…

πŸ‘‰ So to make our code more error proof we've to do something like

// ignore next line if you don't know , assume we're fetching data from server let data = async fetch('https://randomuser.me/api') let name ="";  // now data may or may not come so we've to check it like  if(data) {    if(data.user !== null)    {        if(data.user.name !== "" or data.user.name !== null)        {               let name = data.user.name;        }    } } 
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This code is safer but also more verbose. Our data isn’t even that deeply nested; a more complicated object might have many more levels to check.

πŸ‘‰ Using Optional chaining ?.

let data = async fetch('https://randomuser.me/api')  // using optional chaining let name = data?.user?.name; 
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ As we can see optional chaning (?.) helps us to write minimal and optimized code.

πŸ‘‰ To make it even more simpler , optional chaining looks that if data has user object or it is null. if it finds user object it goes one level down and check for name whether it's available or not. if it finds user null it won't go one level down which results in Cannot read property 'user' of undefined

πŸ‘‰ Unlike manual testing with our code , it won't give us error , rather it returns undefined.


Nullish coalescing ??

πŸ‘‰ According to MDN the nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

πŸ‘‰ in simple words ?? will return right hand side operand if the left hand value is nullish

Can't we do that with || πŸ€”?

πŸ‘‰ The answer is yes definitely but this might not work in some falsy values like "" or NaN

πŸ‘‰a scenario where || won't work…

let cartItems = 0; let msg = "";  let qty = cartItems  || 10; let message = msg|| "hi!"; console.log(qty);     // 10 and not 0 console.log(message); // "hi!" and not "" 
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰the values like "" , 0 , NaN are considered to be falsy value, so logically || is doing it's job , but not serving our purpose ! hence we need to use Nullish coalescing(??)

let cartItems = 0; let msg = "";  let qty = cartItems  ?? 10; let message = msg ?? "hi!"; console.log(qty);     // 0 βœ” console.log(message); // "" βœ” 
Enter fullscreen mode Exit fullscreen mode

Let me know in comment section if you have any doubt or feedback. it's always worth to give time to the thriving developer community πŸ™‚

Keep Coding ❀

Hey Let's Connect πŸ‘‹
Twitter / GitHub

100daysofcodebeginnersjavascriptwebdev
  • 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 2k
  • Popular
  • Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers
  • Author

    Refactoring for Efficiency: Tackling Performance Issues in Data-Heavy Pages

    • 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.