Here is another quick tip on JavaScript, the default value in Javascript. If you are coming from the C# world you may have seen something along the line of
1 |
string str = person.FirstName ?? "Unknown"; |
The above code shows the ?? for null coalescing operator in C#, don’t confused it by the generic default
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
var str = person.firstName || "Unknown"; function add(x, y) { x = x || 0; y = y || 0; return x + y; } alert(add(1)); alert(add('', 1)); alert(add(undefined, 1)); |
In the above code we have create a function call add and we have defined default values inside of the function. Learn it, love it, use it 🙂 It comes in handy to define default values.
Leave A Comment