1591267560
Optional Chaining and Nullish Coalescing
When we write applications, calling properties or methods on a non-existing object can have fatal consequences for your program. This usually results in a reference error causing our code to crash. You may not always expect this to happen, because you are not always in control of the data you are working with.
#nullish-coalescing #es2020 #optional-chaining #javascript
1591267560
Optional Chaining and Nullish Coalescing
When we write applications, calling properties or methods on a non-existing object can have fatal consequences for your program. This usually results in a reference error causing our code to crash. You may not always expect this to happen, because you are not always in control of the data you are working with.
#nullish-coalescing #es2020 #optional-chaining #javascript
1600614480
When Javascript developers think of ECMA, they reference the changes made in 2015 that improved syntax and added new features to the language. However, ECMA still updates the language! And earlier this year, they released ES11, with two of my new favorite tools: Optional Chaining and Null Coalescing!
null is a very interesting datatype. It is a primitive type, along with String, Number, Boolean, Symbol, and Undefined. (Side note: as of ES11, BigInt is now another primitive type!) Primitive types are immutable, while objects (including arrays), can be mutated.
However, null is the odd primitive out. When you use typeof
on any datatype, you are given a reasonable and expected response: typeof 3
is a Number
, typeof {foo: "bar"}
is an Object
, etc. Due to the way Javascript was developed, null is considered an Object
when typeof
is applied to it. Check out this brilliant article by Dr. Axel Rauschmayer for more detail!
null is a falsey value, is loosely equal to undefined (undefined == null // TRUE
), and, most importantly represents the intentional absence of any object value. When a value is not defined, it will always receive the value of undefined. When Javascript developers declare a variable but want it to be empty, they will set the value to null. null is never assigned by Javascript, but only by developers; it is a useful tool to give a value of null if it does not have any meaningful data. Getting a value of null means that your variable is declared and does exist, automatically informing you that you don’t have to adjust variable declaration, hoisting, or scope issues: the value is usable, just void of any information.
#javascript #ecmascript-2020 #null #nullish-coalescing #optional-chaining
1623429540
Python has a lot of built-in tools that allow us to iterate and transform data. A great example is the itertools
module, which offers several convenient iteration functions. Each of these iterator-building functions (they generate iterators) can be used on their own, or combined.
The module was inspired by functional languages such as APL, Haskell and SPL and the elements within itertools
form Python’s iterator algebra.
…
#python #python's itertools #count() #count(), cycle() and chain() #python's itertools – count(), cycle() and chain() #chain()
1603256040
Optional Chaining is one of the newest features in JavaScript. This feature may seem insignificant. However, it can save you a lot of time, code and also a lot of headaches. In this tutorial, you will learn what this feature is about, how it works and how to use it to write better JavaScript code.
Have you ever worked with objects? Then, you know how easy it is to run into following problem. Let’s say you have an object. This object has some properties and maybe also some methods. Next, let’s say that you want to work with some of these properties or methods.
Getting this done is very simple. You can access any property using either dot or square bracket notation. The same applies to methods. What happens is if you try to access some property, or method, that doesn’t exist in that object? When you try to access property that doesn’t exist you will get undefined
.
// Create an object
let myObj = {
name: 'Joe Saladino',
email: 'joe@saladino.io'
}
// Try to access non-existing property "location"
console.log(myObj.location)
// Output:
// undefined
What if you try to access some property that is nested deeper? Imagine you have some object. This object contains some properties. The value of one of these properties is supposed to be also an object. This object should contain some additional properties. What if this nested object doesn’t exist?
What happens if you try to access some property in that non-existing nested object? You will not get undefined
. What will you get instead is a TypeError
. JavaScript will complain that it can’t read property of an object that is not defined.
// Create an object
let myObj = {
name: 'Joe Saladino',
email: 'joe@saladino.io'
}
// Try to access non-existing property "location"
console.log(myObj.location)
// Output:
// undefined
// Try to access non-existing property "city"
// in non-existing object "location"
console.log(myObj.location.city)
// Output:
// TypeError: Cannot read property 'city' of undefined
#javascript #optional chaining
1591585558
A Practical Example of Nullish Coalescing
Nullish coalescing is a brand new logical operator (??) for the JavaScript programming language. It was released along with a whole slew of other new features on April 2nd this year as part of ECMAScript version 2020 (or ES2020).
#es2020 #nullish-coalescing #javascript-tips #javascript #js