Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return null in Javascript function

Tags:

javascript

Learning Javascript functions, and having trouble with the problem below.

Modify this function so that if it’s called with 1 or 0 arguments, it returns null.

function divide(num1, num2) {
    return num1 / num2;
}
let num1 = null;
let num2 = null;
let answer = divide(1,1)
console.log(answer);
like image 473
Jon Avatar asked Feb 09 '18 19:02

Jon


2 Answers

TL;DR

Assuming we only need to apply a straight forward modification, I would avoid too much syntactical sugar and just write it this way:

function divide(num1, num2) {
  if (arguments.length < 2) {
    return null;
  }
  return num1/num2;
}

If we want to make it simple and elegant, I would write it this way (requires ES6 features):

const divide = (...args) => args.length < 2 ? null : args[0] / args[1];

Explanation

Improvement steps in vanilla JS (ES5 and before)

  • Using function arguments: this is simply an array like object that will magically appear inside your function, which contains the arguments you have passed to the function.

function divide(num1, num2) {
  if (arguments.length < 2) {
    return null;
  }
  return num1/num2;
}

While this is a great solution for the problem there are a downsides to it, if you wanted to switch to arrow function arguments object doesn't appear.

  • **Using ternary operator**you can farther more reduce the code by using the ternary ? which is best suitable for simple if statements

function divide(num1,num2) {
  return arguments.length < 2 ? null : num1 / num2;
}

Improvement steps in ES6

  • Using spread (in this case it is actually called rest): the ... can be used to either collect items into an array known as rest, or expand an array known as spread.

function divide(...args) {
  return args.length < 2 ? null : args[0] / args[1];
}

JavaScript will collect all the arguments passed to the function and put them into an array called args, I like this better since the reader can see where is args defined.

  • using arrow function: there are many difference between arrow and normal function, but many prefer it since it is shorter, and since we have a one-liner function why not use it.

const divide = (...args) => args.length < 2 ? null : args[0] / args[1];

On a final note all the previous solutions has a downside that we are only checking for length of arguments but not contents of arguments, lets assume that someone sent undefined into one of the first two arguments, you'll have 2 arguments but one of them is kinda missing and you'll get NaN since number of arguments is 2.

function divide(num1, num2) {
	  if (num1 === undefined || num2 === undefined) {
return null;
  }
  return num1/num2;
}

Demo

function divide1(num1, num2) {
	  if (arguments.length < 2) {
	    return null;
	  }
	  return num1/num2;
	}
	
function divide2(num1,num2) {
	  return arguments.length < 2 ? null : num1 / num2;
	}
	
	
function divide3(...args) {
	  return args.length < 2 ? null : args[0] / args[1];
	}


const divide4 = (...args) => args.length < 2 ? null : args[0] / args[1];


const test = (cb) => {
  console.log("-------->" + cb.name)
  console.log(cb());
  console.log(cb(1));
  console.log(cb(1, 2));
  console.log(cb(1, undefined));
  console.log(cb(1, null));
  console.log(cb(1, 2, 3));
};

test(divide1);
test(divide2);
test(divide3);
test(divide4);
like image 88
oqx Avatar answered Nov 18 '22 13:11

oqx


Peep this.

function divide(num1, num2) {
    if(arguments.length < 2) return null;
    return num1 / num2;
}

The arguments object is available in all (non-arrow) function and represents all the arguments passed into the function. It has a length property that tells you how many arguments there are.

like image 26
I wrestled a bear once. Avatar answered Nov 18 '22 12:11

I wrestled a bear once.