You could simplify the code a bit and return null if there's no more than one iteration to check the prefix. Also you could add an extra check for empty or 1 word arrays, otherwise your code would fail.

function prefix(words) {
    if(words.length < 2 || words[0] === '') { // extra check for 0 or 1 words
      return words[0] || null;
    }
    let i = 0;
    while (true) {
        const c = words[0][i];
        for (let j = 1; j < words.length; j++) {
            if (c != words[j][i]) {
                return i ? words[0].slice(0, i) : null;
            }
        }
        i++; // this will happen when at least 1 character is common
    }
}
console.log(prefix([]));
console.log(prefix(['']));
console.log(prefix(['', '']));
console.log(prefix(['flower', '']));
console.log(prefix(['flower']));
console.log(prefix(["flower", "flowers", "floyd", "flow", "floor"]))
console.log(prefix(["flower", "flowers", "floyd", "flow", "sunflower", "floor"]))

And a benchmark:

<script benchmark data-count="10000000">

const words = ["flower", "flowers", "floyd", "flow", "floor"];

// @benchmark museum/tom
    function prefix(words) {
      for (let i = 0; i < words[0].length; i++) {
        const char = words[0][i];
        for (let j = 1; j < words.length; j++) {
          // first letter unequal
          if (i === 0 && words[j][i] !== char) {
            return null;
          }
          if (i === words[j].length || words[j][i] !== char) {
            return words[0].substring(0, i);
          } 
        }
      }
      // No common prefix found
      return null;
    }
    // @run
    prefix(words);

// @benchmark Alexander
    function prefix2(words) {
        if(words.length < 2 || words[0] === '') { // extra check for 0 or 1 words
          return words[0] || null;
        }
        let i = 0;
        while (true) {
            const c = words[0][i];
            for (let j = 1; j < words.length; j++) {
                if (c != words[j][i]) {
                    return i ? words[0].slice(0, i) : null;
                }
            }
            i++;
        }
    }
    // @run
    prefix2(words);
</script>
<script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>

Answer from Alexander Nenashev on Stack Overflow
Top answer
1 of 4
2

You could simplify the code a bit and return null if there's no more than one iteration to check the prefix. Also you could add an extra check for empty or 1 word arrays, otherwise your code would fail.

function prefix(words) {
    if(words.length < 2 || words[0] === '') { // extra check for 0 or 1 words
      return words[0] || null;
    }
    let i = 0;
    while (true) {
        const c = words[0][i];
        for (let j = 1; j < words.length; j++) {
            if (c != words[j][i]) {
                return i ? words[0].slice(0, i) : null;
            }
        }
        i++; // this will happen when at least 1 character is common
    }
}
console.log(prefix([]));
console.log(prefix(['']));
console.log(prefix(['', '']));
console.log(prefix(['flower', '']));
console.log(prefix(['flower']));
console.log(prefix(["flower", "flowers", "floyd", "flow", "floor"]))
console.log(prefix(["flower", "flowers", "floyd", "flow", "sunflower", "floor"]))

And a benchmark:

<script benchmark data-count="10000000">

const words = ["flower", "flowers", "floyd", "flow", "floor"];

// @benchmark museum/tom
    function prefix(words) {
      for (let i = 0; i < words[0].length; i++) {
        const char = words[0][i];
        for (let j = 1; j < words.length; j++) {
          // first letter unequal
          if (i === 0 && words[j][i] !== char) {
            return null;
          }
          if (i === words[j].length || words[j][i] !== char) {
            return words[0].substring(0, i);
          } 
        }
      }
      // No common prefix found
      return null;
    }
    // @run
    prefix(words);

// @benchmark Alexander
    function prefix2(words) {
        if(words.length < 2 || words[0] === '') { // extra check for 0 or 1 words
          return words[0] || null;
        }
        let i = 0;
        while (true) {
            const c = words[0][i];
            for (let j = 1; j < words.length; j++) {
                if (c != words[j][i]) {
                    return i ? words[0].slice(0, i) : null;
                }
            }
            i++;
        }
    }
    // @run
    prefix2(words);
</script>
<script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>

2 of 4
0

Here's what's happening

In the case of ["flower", "flowers", "floyd", "flow", "sunflower", "floor"], the loop iterates through the characters of the first word and compares them to the other words.

When i reaches the length of the first word (i = 5), there is no exit condition for the outer loop, so it continues to the next iteration.

However, there's no corresponding character in the other words, so the inner loop conditions (i === words[j].length || words[j][i] !== char) are not met.

Since the loop completes without returning anything and all the words are not the same, the function ends without a return statement.

In JavaScript, when a function doesn't return anything, it returns undefined, and that's why you see nothing printed.

The fix for this is to add a return statement after the outer loop to handle the case where the loop completes without finding a common prefix.

function prefix(words) {
    /* Check if words list is 0, since prefix will always be null
    in this case */
    if (words.length === 0) return null; 

    for (let i = 0; i < words[0].length; i++) {
        const char = words[0][i];
        for (let j = 1; j < words.length; j++) {
            if (i === words[j].length || words[j][i] !== char) {
                if (i === 0) return null;
                return words[0].substring(0, i);
            }
        }
    }

    return null; // Add this line to handle the missing case
}

console.log(
    prefix(['flower', 'flowers', 'floyd', 'flow', 'flinging', 'floor'])
); // output: null

console.log(
    prefix(['triangle', 'trick', 'trivial', 'triceratops', 'tricycle']) // output: 'tri'
);

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript | MDN
The null keyword refers to the null primitive value, which represents the intentional absence of any object value. function getVowels(str) { const m = str.match(/[aeiou]/gi); if (m === null) { return 0; } return m.length; } console.log(getVowels("sky")); // Expected output: 0
Top answer
1 of 3
8

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);

2 of 3
2

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.

🌐
JavaScript Tutorial
javascripttutorial.net › home › an essential guide to javascript null
An Essential Guide to JavaScript null
September 29, 2020 - const square = null; if (square) { console.log('The square is not null'); } else { console.log('The square is null'); }Code language: JavaScript (javascript) ... In this example, the square variable is null therefore the if statement evaluates it to false and executes the statement in the else clause. The typeof value returns the type of the value.
Top answer
1 of 12
183

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?

2 of 12
88

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.

🌐
Codecademy
codecademy.com › forum_questions › 50735705d9609d000201ca83
Why is my function returning null? | Codecademy
I know this is not the most common construction, but it is one i am able to compose logically…. its returning NULL … any ideas? var taxiFare = function (milesTraveled, hourOfDay) { var baseFare = 2.50; var milesTraveled var costPerMile = 2.00 var hourOfDay var cost = baseFare + (costPerMile * milesTraveled) + nightSurcharge; var nightSurcharge = function (hourOfDay){ if (6 < hourOfDay || hourOfDay >=20){ return 0 } else { return 0,50 } } return cost; } taxiFare (10,10) ... First of all, you don’t need to define milesTraveled and hourOfDay inside the function with var milesTraveled and var hourOfDay, because you’re already inputting them as parameters.
🌐
Medium
vvkchandra.medium.com › essential-javascript-mastering-null-vs-undefined-66f62c65d16b
Essential JavaScript: Mastering null vs undefined | by Chandra Gundamaraju | Medium
September 9, 2020 - So what’s in result then? JavaScript returns undefined by default when a function doesn’t return any other value. If a function returns null explicitly, then null is returned, not undefined.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › null-in-javascript
Null in JavaScript - GeeksforGeeks
June 5, 2024 - ... class Square { constructor(length) { this.length = length; } get area_of_square() { return Math.pow(this.length, 2); } // Static function that returns the length static create_function(length) { return length > 0 ?
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-null
Everything about null in JavaScript
null might appear, often unexpectedly, in situations when you expect an object. Then if you try to extract a property from null, JavaScript throws an error. Let's use again greetObject() function and try to access message property from the returned object:
Top answer
1 of 1
1

You can eventually do it by parsing the ECMAScript Language Specification which conviniently provides the implementation of the native methods.

For instance, Object.defineProperty(O, P, Attributes) is documented like this:

  1. If Type(O) is not Object, throw a TypeError exception.
  2. Let key be ? ToPropertyKey(P).
  3. Let desc be ? ToPropertyDescriptor(Attributes).
  4. Perform ? DefinePropertyOrThrow(O, key, desc).
  5. Return O.

Here, simply by parsing the list, you can determine that:

  • The method actually returns something, because one of the elements from the list matches the Return .*\. pattern.

  • The method returns the parameter O, because O is among the arguments of the method.

Similarly, Array.prototype.find very conviniently tells you that sometimes, the method:

  1. Return[s] undefined.

The difficulty is that even this simplest example isn't easy to perform programmatcially. I suppose that while ECMAScript is very consistent in its descriptions of the methods, the high number of possible forms of those methods make any parsing of the documentation rather complex. For instance, Array.prototype.join returns R, but R is not an argument: instead, it is a value which is assigned through the statements such as:

  1. If element0 is undefined or null, let R be the empty String; otherwise, let R be ? ToString(element0).

Unless there is an actual BNF grammar that was used to write the spec, parsing such statements would be difficult to impossible.

The good side, however, is that if you achieve to create such parser, it will include the “edge cases, including functions used improperly” you were talking about.

My use case that inspired my question was coding a "maybe" monad that would need to deal with null or undefined coming from any source, and I was just interested in creating a few different demo functions that would do so.

In my humble opinion, creating the list of all JavaScript methods just to have “a few different demo functions” is an overkill. Why not limiting yourself by one or several methods and to use them as a demo?

Still using the spec, an example of a method which returns undefined is Array.prototype.find already listed above; for null, an example could be Date.prototype.toJSON.

🌐
Web Dev Simplified
blog.webdevsimplified.com › 2021-01 › null-vs-undefined
Null Vs Undefined
To start I want to explain what null and undefined have in common since they are very similar. Both null and undefined mean that there is no value. If a variable is set to null or undefined it has no value and if a function returns null or undefined then it is saying it has no value to return.
🌐
Reddit
reddit.com › r/askprogramming › should i return null or an empty object?
r/AskProgramming on Reddit: Should I return null or an empty object?
July 25, 2023 -

My company uses files that are essentially merged XML files into one. I wrote a pretty basic parser and node object that contains the tag name, text, and child node objects. Recently I ran into an error where the file couldn't be found, and my function that searches for child nodes returned null (as it should). But it caused a runtime error, so I'm trying to figure out the best way to address issue. Should I return an empty object so that the code doesn't crash, or should I continue returning null and wrap it in a try/catch?

🌐
Medium
medium.com › @jadhavsid1101 › why-returning-null-from-functions-is-a-bad-idea-and-what-to-do-instead-d3074aa3c3b1
Why Returning Null from Functions is a Bad Idea and What to Do Instead | by Siddhesh Jadhav | Medium
May 25, 2023 - Returning null can cause several problems in software development. Firstly, it can cause NullReferenceExceptions. If the caller of a function that returns null is not aware of the possibility of a null return value, they may try to access a property or method on the null object, which will result in a NullReferenceException.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
I keep getting "null", but I need a "0" to show - JavaScript - The freeCodeCamp Forum
January 3, 2023 - I need to count vowels (a, e, i, ... code: function getCount(str) { let countVowels = /[aeiou]/g let result = str.match(countVowels) let final = result.length if (final > 0) { return result.length; } else { return "0" } } I have also ...