Sure! Here it is separated in parts and with comments:
// We create a plain object that has a key = value mappingconstfontWeightMap={// For example, here we say: When the value is 100, we want "Super Thin"100:"Super Thin",300:"Thin",400:"Normal",600:"Bold",900:"Heavy"};// The value of fontWeight here will be "Thin"constfontWeight300=fontWeightMap[300];// But maybe we have a value that is not mapped, like 200constfontWeight200=fontWeightMap[200];// That produces an undefined, so we use the ?? operator// that only goes to the right value if the left is null or undefined// Here because 200 is undefined, the value of safeFontWeight is "default"constsafeFontWeight=fontWeightMap[200]??"default";
Putting all together:
constfontWeight=// Map Object{100:"Super Thin",300:"Thin",400:"Normal",600:"Bold",900:"Heavy"// We pass a weight value, and if it doesn't exist then we go to ""}[weight]??"";
We're a place where coders share, stay up-to-date and grow their careers.
Can you elaborate more on how the #2 works?
Sure! Here it is separated in parts and with comments:
Putting all together: