function abTest(a, b) return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));abTest();

You are watching: Basic javascript: return early pattern for functions

Taking the square root of a an adverse number in JavaScript should already be returning NaN. But, right here is one means you may check the an outcome of the calculation, and also then return a tradition string unknown in this case:

function abTest(a, b) var result = Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2)); return !isNaN(result) ? result : "undefined";console.log(abTest(2, 2));console.log(abTest(-2, 2));

Since javascript square root of a an adverse number together NAN .You deserve to use the below duty to check if any type of of the passed debate is an unfavorable number. If any of the number is negative you can return "undefined".

function abTest(a, b) { return (a

Firstly, while returning an undefined value you don"t need to define the word "undefined" as it is a keyword. It just takes a return keyword v a semi-colon as defined below, rest is just the logic according to the requirement:

if (a

many thanks for contributing response to stack Overflow!Please be sure to

*answer the question*. Administer details and share her research!

But *avoid* …

To learn more, see our advice on writing great answers.

short article Your prize Discard

By clicking “Post her Answer”, friend agree to our regards to service, privacy policy and also cookie policy

## Not the answer you're looking for? Browse various other questions tagged javascript return-value or questioning your very own question.

See more: Download 21 Savage No Heart Free Mp3 Download 21 Savage & Metro Boomin

site architecture / logo design © 2021 stack Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.10.27.40585

your privacy

By clicking “Accept every cookies”, you agree stack Exchange have the right to store cookie on your machine and disclose details in accordance v our Cookie Policy.