DEV Community

Discussion on: One life saving Javascript Tip

Collapse
 
kepta profile image
Kushan Joshi

This early exit post was inspired by the countless number of functions I have seen which do something inside knees deep { { { brackets.

// Comparison
function early(data) {
  if (!data) {
    return data;
  }

  return data.map(r =>
    r
      .split(',')
      .join('-')
      .slice(1)
  );
}
function late(data) {
  if (data) {
    return data.map(r =>
      r
        .split(',')
        .join('-')
        .slice(1)
    );
  } else {
    return data;
  }
}