dev-resources.site
for different kinds of informations.
Difference between ? and ?? in JavaScript/Typescript
Published at
10/26/2022
Categories
javascript
operators
optional
typescript
Author
saimwebhr
Author
9 person written this
saimwebhr
open
Hey folks, if you have opened this article that means you are curious about these 2 operators, how they operate, and what's the difference between these two. So without wasting any time, let's jump into details with a code example.
?
? is Optional Chaining Operator, also commonly named as null checker for objects. Its primary use is to check if an object exists or not.
Example
const user = {
id: 5,
name: "John"
};
console.log(user?.name); //John
console.log(user?.fullName); //undefined, program won't crash.
console.log(user.fullName);
//TypeError: Cannot read property โfullNameโ of undefined
??
?? is Nullish Coalescing Operator. It is used to initialize an object/variable if it is undefined or null.
Example
const user = {
id: 5,
name: "",
firstName: null,
lastName: undefined
};
console.log(user.name??"Johnny Depp");// prints ""
console.log(user.firstName??"Johnny");// prints Johnny
console.log(user.lastName??"Depp");// prints Depp
Feel free to add suggestions in the comments.
Thanks. Happy Coding.
operators Article's
30 articles in total
Essential MySQL Operators and Their Applications
read article
Exposing replica nodes in Percona Operator for PostgreSQL
read article
Itโs just โ,โ โ The Comma Operator
read article
Operators, Conditionals and Inputs
read article
Practical Guide to Python Conditional Statements
read article
Python Operators Demystified
read article
SQL Operators Made Easy for Beginners
read article
First Steps in SQL Operators: A Beginner's Guide
read article
AND / OR operators, Short-Circuiting and Nullish Coalescing in Javascript
read article
From Zero to Hero: Disaster Recovery for PostgreSQL with Streaming Replication in Kubernetes
read article
Google Search Operators & Usage Tips
read article
Operators in C programming
read article
MySQL Operators โ A Guide
read article
Annotations in Kubernetes Operator Design
read article
Exploring the unusual: JavaScript arrays and the 'in' operator
read article
Install Kubernetes Controllers via Operators - ARGO CD
read article
Mastering Advanced JavaScript Operators: The Ultimate Guide
read article
Operators in JavaScript: The Fundamentals
read article
Dart as, is, is! operatรถrleri
read article
Nullish Coalescing Operator
read article
Difference between ? and ?? in JavaScript/Typescript
currently reading
Ordering Event Bus Events with RxJS and concatMap
read article
Division, Floor Division and Modulus - Python Arithmetic Operators every beginner should know.
read article
Operators in Python
read article
Angular - Rxjs - Operator mergeAll
read article
Angular - Rxjs - Operator map
read article
Swift โ 11 Useful Combine Operators You Need to Know
read article
Cloud Native CICD Pipelines in OpenShift
read article
Kubernetes Operators to realize the dream of Zero-Touch Ops
read article
JavaScript Basic - Variable, Data Types, Operators, Comparisons
read article
Featured ones: