Undefined typically refers to something which has not yet been assigned a value (yet). Null refers to something which definitively has no value. In that case, I would recommend returning a null. Note that a function with no specified return value implicitly returns undefined.
From the ECMAScript2015 spec
4.3.10 undefined value
primitive value used when a variable has not been assigned a value
4.3.12 null value
primitive value that represents the intentional absence of any object value
http://www.ecma-international.org/ecma-262/6.0/#sec-terms-and-definitions-undefined-type
Further reading:
When is null or undefined used in JavaScript?
Answer from chiliNUT on Stack OverflowUndefined typically refers to something which has not yet been assigned a value (yet). Null refers to something which definitively has no value. In that case, I would recommend returning a null. Note that a function with no specified return value implicitly returns undefined.
From the ECMAScript2015 spec
4.3.10 undefined value
primitive value used when a variable has not been assigned a value
4.3.12 null value
primitive value that represents the intentional absence of any object value
http://www.ecma-international.org/ecma-262/6.0/#sec-terms-and-definitions-undefined-type
Further reading:
When is null or undefined used in JavaScript?
I will give you my personal opinionated way of choosing between the two.
My simple question is: could the value, given another input/state/context be defined to something?
If the answer is yes then use null else use undefined. More generally any function returning an object should return null when the intended object does not exist. Because it could exist given another input/state/context.
null represents the absence of value for a given input/state/context. It implicitly means that the concept of the value itself exist in the context of your application but may be absent.
In your example the concept of a next card exists but the card itself may not exist. null should be used.
undefined implicitly represents the absence of meaning of that value in your application's context. For example, if I manipulate a user object with a given set of properties and I try to access the property pikatchu. The value of this property should be set to undefined because in my context it doesn't make any sense to have such a property.
Why function is returning undefined
javascript - Simple function returning 'undefined' value - Stack Overflow
Why is my code returning undefined?
How to avoid returning undefined from a function that catches error?
Videos
Other people have given good, correct answers but I want to be explicit about why, since it might not be obvious to some people (not directed at the OP).
A function is nothing more than a set of steps for the computer to take.
This is known as a function call:
getSmallestDivisor(121)
Anytime the return keyword is used, the function stops and replaces the function call with whatever comes after that return word (it could be nothing).
So in this case, the problem with the original function is that when the script reaches this line...
getSmallestDivisor(xSqrt);
...it returns 11 to that function call, which never gets returned to the original function call that happened inside of alert().
So the solution is simply to add a return before the one where it calls itself.
return getSmallestDivisor(xSqrt);
This is a common mistake when making recursive functions. A good way to help figure out what is going on is to make extensive use of the browser console.
function getSmallestDivisor(xVal) {
console.log("This is xVal: " + xVal);
if (xVal % 2 === 0) {
console.log("xVal % 2 === 0 was true");
return 2;
}
else if (xVal % 3 === 0) {
console.log("xVal % 3 === 0 was true");
return 3;
}
else {
console.log("This is else.");
var xSqrt = Math.sqrt(xVal);
console.log("This is xSqrt of xVal: " + xSqrt);
if (xSqrt % 1 === 0) {
console.log("xSqrt % 1 === 0 was true... recursing with xSqrt!!!");
getSmallestDivisor(xSqrt);
}
else {
console.log("This is the else inside of else. I am returning: " + xVal);
return xVal;
}
}
}
var y = getSmallestDivisor(121);
console.log("This is y: " + y);
Now in your browser, you can open the console (Option + Command + I in most browsers on macOS) and watch what is happening - which parts get executed, etc.
if (xSqrt % 1 === 0) {
return getSmallestDivisor(xSqrt); // missing return here
} else {
return xVal;
}
Demo: Fiddle
I have several functions, which throws errors. What I found frustrating, is that the function would return undefined, if there is no return statement after catch clause, and in the future call stack I should handle the undefined value. Although it makes sense to be so, can this be avoided somehow? If not, can somebody explain me why this can not be avoided?
For example, I have this function:
export function parseStringToFormat(
initialDateStr: string,
initialDateFormat: string,
desiredFormat: string,
referenceYear: number
): string {
try {
const initialDateFormatParts: DatePart[] = computeFormatParts(initialDateFormat);
const initialDateNumbers: EditDateNumbers = computeDateNumbers(initialDateStr, initialDateFormatParts, referenceYear);
const separatorIndex: number = initialDateStr.search(ALLOWED_SEPARATORS_PATTERN);
const separator: string = initialDateStr[separatorIndex];
const formatParts: DatePart[] = computeFormatParts(desiredFormat);
const finalDateStr: string = computeStringBasedOnNumbers(initialDateNumbers, formatParts, separator);
return finalDateStr;
} catch (e: unknown) {
console.error(e);
}
}And any of the used function within it can throw errors.
Obviously, the IDE tells me that the function does not include undefined in its return type.
Thanks!
Removing the return keyword will result in the outcome i want but i dont quite understand why adding a return to the code block will result in undefined?
function reverseArray(sentence){
let newArray=[]
for(let i= sentence.length-1; i >= 0; i--){
return
newArray.push(sentence[i])}
return newArray}
const sentence = ['sense.','make', 'all', 'will', 'This'];
console.log(reverseArray(sentence))