Why String.prototype.match() returns null instead of empty array?

lazydev

Example taken from MDN webdocs

var paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
var regex = /[A-Z]/g;
var found = paragraph.match(regex);

console.log(found); // will return an array of matches and returns null when nothing matches.

Would like to know reasoning behind returning null instead of an Empty Array when nothing matches?

zerkms

That's how String.prototype.match is defined in the EcmaScript standard

  1. 21.1.3.11String.prototype.match ( regexp )
  2. 21.2.5.7RegExp.prototype [ @@match ] ( string )
  3. 21.2.5.2.2Runtime Semantics: RegExpBuiltinExec ( R, S )

In short: if nothing matches - it returns null by the standard.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

string.match() returns a String instead of an Array

why Object.keys(Array.prototype) returns empty array?

Why null statement ToString() returns an empty string?

Absinthe returns an array that contains one null value instead of an empty array

Match returns empty string

Why StreamReader returns string instead of byte array

Convert.ToString returns string.empty instead of null

Need to understand why console log returns an empty string instead of undefined

Why is the default value of the string type null instead of an empty string?

Why Object.keys is returns array of string instead of array of Numbers

Array.prototype.filter() returns empty list

why if null function in mysql returns value as string instead of integer

Return Empty String Instead of Null

Return empty array instead of null

Why is this function returns an empty array?

Why is this string array empty?

RestAssured returns array instead of String

Why does an empty string literal in a multidimensional array decay to a null pointer?

mysql - Query with IFNULL and CONCAT_WS returns empty string instead predefined IFNULL parameter in case of NULL

SQL XML .node .query() returns empty string instead of NULL if table node exist

Regex: match an empty string instead of nothing

PDO returns false instead of empty array

Mongodb lookup with match in aggregate returns an empty array

Why findOneBy returns null instead of a result?

Want to send empty json string "{}" instead of "null"

Golang Insert NULL into sql instead of empty string

ActiveRecord + ActiveAdmin, save a empty string instead of null

Laravel / League Fractal returns an empty array instead of empty object

Array.prototype.map() returns empty item but Array.prototype.forEach() doesn't