-3

Reading highstock source codes, but confused the following codes:

textLineHeight = textStyles && textStyles.lineHeight,

wrapper.height = (height || bBox.height || 0) + 2 * padding;

cHeight = (old && chart.oldChartHeight) || chart.chartHeight;

Thanks

BAE
  • 8,550
  • 22
  • 88
  • 171

1 Answers1

3
textLineHeight = textStyles && textStyles.lineHeight;

means set textLineHeight to textStyles.lineHeight if textStyles is truthy and it has a property lineHeight which is also truthy.

&& is often referred to as the guard operator in JavaScript, since its short circuiting evaluation ensures that textStyles.lineHeight will not be evaluated unless textStyles is defined. This avoids an error if it may indeed be undefined.

wrapper.height = (height || bBox.height || 0) + 2 * padding;

Here, the || means use height if it is truthy otherwise use bBox.height if that is truthy, and finally fallback to 0 if neither are truthy. || is often called the default operator in JavaScript as it allows one to specify a value to use in case the expression on its left hand side is falsy.

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84