Skip to main content

Command Palette

Search for a command to run...

JavaScript Amazing operators

1. Nullish Coalescing Operator (??) 2. Logical Nullish Assignment (??=) 3. Optional Chaining Operator (?.)

Updated
JavaScript Amazing operators
C

CapsCode: Empowering Innovation Through Education and Technology

CapsCode is a pioneering edtech and web development company committed to transforming the lives of students and businesses alike. Founded with the vision of creating a brighter, tech-driven future, CapsCode empowers students by providing world-class coding education while enabling businesses to achieve their goals with cutting-edge IT solutions.

For Students At CapsCode, we believe that coding is the language of tomorrow. Through engaging and industry-relevant courses, personalized mentoring, and hands-on workshops, we help students develop: Problem-solving skills: Preparing them for real-world challenges. Technical expertise: Equipping them with practical knowledge of coding and web development. Confidence in technology: Making them future-ready for careers in IT and beyond.

We foster an environment where students not only learn to code but also learn to innovate, collaborate, and think critically.

For Businesses CapsCode offers bespoke IT solutions tailored to the unique needs of businesses. From responsive web development to advanced digital marketing strategies, our services are designed to help companies grow their online presence and streamline their operations. Our key offerings include: Custom website and software development E-commerce platform creation Lead generation and digital marketing Professional email and Google Business setup

We act as partners in innovation, helping businesses leverage technology to reach new heights.

Our Mission Our mission is to empower individuals and organizations by bridging the gap between technology and accessibility. We aim to inspire a new generation of coders while helping businesses thrive in a competitive digital landscape.

Why Choose CapsCode? Over 9 years of IT industry expertise. A blend of education and professional IT services under one roof. A commitment to fostering growth, innovation, and success.

Join us in shaping the future—whether you're a student eager to unlock your potential or a business ready to embrace the possibilities of technology.

Learn. Build. Grow. With CapsCode.

JavaScript Amazing operators

Hello Everyone, In this tutorial I am going to discuss about 3 amazing operators that is very useful and time saving operator that most of beginner developers don't know. The 3 operator are,

  1. Nullish Coalescing Operator (??)
  2. Logical Nullish Assignment (??=)
  3. Optional Chaining Operator (?.)

We are going to discuss each of these operator one by one with some of the examples for better understanding.

1. Nullish Coalescing Operator (??)

syntax: a??b

  • If a is defined then the output will be a
  • if a is not defined/ Nullish (NULL or UNDEFINED) then the output will be b

In other words, Nullish Coalescing Operator(??) returns the first argument if it is NOT null or undefined. Otherwise returns second argument

Examples:

let a=NULL
console.log(a??50) //50
console.log(a) //NULL
let a=10
let c=30
console.log(a??b??c??d) //10
//gives output, the first defined value

2. Logical Nullish Assignment (??=)

syntax: a ??= b the above syntax is equivalent to a ?? (a=b)

  • if a is not NULLISH (Null or Undefined), then output will be a
  • if a is NULLISH, then output will be b, and value of b is assigned to a

example:

let a=NULL
console.log(a??=50) //50
console.log(a) //50
//compare the output of a from the previous example.

3. Optional Chaining Operator (?.)

syntax: obj ?. prop

  • is similar to obj.prop if the value of obj exist,
  • otherwise, if value of obj is undefined or null it returns undefined.

By using ?. operator with object instead of using only dot (.) operator , JavaScript knows to implicitly check to be sure that that obj is not null or undefined before attempting to access obj.prop
NOTE: obj can be nested as well like obj . name ?. firstname

example:

let obj= {
   person:{
       firstName:"John",
       lastName:"Doe"
   },

   occupation: {
       compony:'capscode',
       position:'developer'
   },
   fullName: function(){
       console.log("Full Name is: "+ this.person.firstName+" >"+this.person.lastName)
  }
}
console.log(obj . person . firstName) //John
console.log(obj . human . award) //TypeError: Cannot read property 'award' of undefined
console.log(obj ?. human . award) //TypeError: Cannot read property 'award' of undefined
console.log(obj . human ?. award) //undefined
delete obj?.firstName;  // delete obj.firstName if obj exists
obj . fullName ?. () //logs John Doe
obj ?. fullName() //logs John Doe
obj . fullDetails() // TypeError: obj.fullDetails is not a function
obj ?. fullDetails() // TypeError: obj.fullDetails is not a function
obj.fullDetails ?. () //undefined

summing up these, The optional chaining ?. syntax has three forms:

  1. obj?.prop – returns obj.prop if obj exists, otherwise undefined.
  2. obj?.[prop] – returns obj[prop] if obj exists, otherwise undefined.
  3. obj.method?.() – calls obj.method() if obj.method exists, otherwise returns undefined.

Hope you all like this and this post will be informative and helpful for you in your next project. If you have any query or doubt, fell free to contact us.

Please visit https://www.capscode.in/#/ for more info or follow us on Instagram https://www.instagram.com/capscode/

Thank You,

CapsCode

A

So nice bro! Can i repost on my blog.

1
C
CapsCode5y ago

Thank you, Yes you can repost it anywhere....just give us a credit. It will be helpful for us as well.