

Difference between ? and ?? in JavaScript/Typescript
source link: https://dev.to/saimwebhr/difference-between-and-in-javascripttypescript-f4d
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Difference between ? and ?? in JavaScript/Typescript
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.
Recommend
-
14
JavaScript: The difference between match() and matchAll() String.prototype.match() String.prototype.match() is a meth...
-
10
The difference between any and unknown type in TypeScriptPublished: 2020.12.25 | 2 minutes readBoth any and unknown are universal types in TypeScript that allow you to assign whatever the heck you wan...
-
10
What's the difference between these two method of defining a 'class' in JavaScript? Method One Define method within the constructor: function MyClass() { this.foo = function() { console.log('hello world!');...
-
14
In JavaScript, you can pass by value and by reference. The main difference between the two is that passing by value happens when assigning primitives while passing by reference when assigning objects. Let’s discuss values and...
-
8
TypeScript: the difference between interface and type Sign inWelcome!Log into your accountyour usernameyour password
-
10
The Difference Between a Class and a Prototype in JavaScriptJuly 14th 2021 new story8
-
7
Difference between SLICE & SPLICE in JavaScript Hello Devs, In this article, we will discuss what's the difference between the two most important methods of Array in JavaScript (i.e Slice and Splice) The...
-
5
Hello Everyone, The difference between for-of and for-in loop really troubled me when I was learning JavaScript. And with this blog I will try to clear the confusion once and for all. Let's understand them one by one.
-
5
Whenever I encounter null or undefined, I get incredibly confused by how each data type is used in JavaScript. I mean, what's the difference? Don't they both express the concept of nothing?
-
8
The difference between “undefined” and “not defined” in JavaScriptHow two words can decide if your code breaks or not
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK