How to return null value in Javascript function

Question Noob. Learning Javascript functions and the problem with the problem below.

Modify this function so that if it is 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);

Thank!

+4
source share
3 answers

TL DR

Assuming we only need to apply direct change, I would avoid too syntactic sugar and simply write it like this:

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

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

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

Description

Improvement Steps in Vanilla JS (ES5 and earlier)

  • : , , , , .

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

, , arguments, .

  • ** ** , ?, if statement

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

ES6

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

JavaScript , , args. , , args.

  • : , , , , .

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

, , , , - undefined , 2 , , NaN, 2.

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

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);
Hide result
+3

.

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

arguments (-) , . length, , .

+2

Technically, passing to null will call it with two arguments, so your example doesn't exactly represent this. If you need an answer that is safe for arrow functions (they don't have an object arguments), you can simply check if the second argument is a number. It will catch a lot.

function divide(num1, num2) {
    if (typeof num2 !== 'number') return null;
    return num1 / num2;
}

If you want to handle the edge when the first argument passed was undefined, you can check both arguments.

function divide(num1, num2) {
    if (typeof num1 !== 'number' || typeof num2 !== 'number') return null;
    return num1 / num2;
}

If you want a fantasy, it could be a single line arrow:

const divide = (num1, num2) => typeof num2 !== 'number' ? null : num1 / num 2;
0
source

Source: https://habr.com/ru/post/1693433/


All Articles