Is this a 'dead code' i.e. is it just redundant?

Viewed 60

I came across this code snippet in our code base:

function initDoughnutChart(target) {
  var labels = $(target).data("labels").toString().split(",");
  var dataString = $(target).data("chart-data").toString();
  // single-entry values
  if (typeof dataString === 'number') {
    dataString = dataString.toString();
  }
  var data = dataString.split(",");
  ...

Am I right to think the if statement is redundant as dataString is initialised to the value <something>.<something>.toString() thus it will (always?) have a type of 'string' (unless its initialisation throws but then the if won't be reached at all).

1 Answers

I think you're right. The only way I can see datastring as a number would be to have a custom toString defined somewhere else in the code (and I see no reason why).

Of course, there are other caveats in the code, like the fact that $(target).data("labels"). will not work if target is undefined.

Related