Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript replace null

Tags:

javascript

var p = "null"
var q = null;
(p == q) //false. as Expected.

p.replace(null, "replaced") // outputs replaced. Not expected.
p.replace("null", "replaced") //outputs replaced. Expected.

q.replace(null, "replaced") // error.  Expected.
q.replace("null", "replaced") //error. Expected.

Why? Does replace not differentiate between "null" and null?

I ask because I ran into a bug in angularjs:

replace((pctEncodeSpaces ? null : /%20/g), '+');

If for example, someone had a username of "null" and it was used as url, it would be replaced with "+" on any $http calls. e.g. GET /user/null.

Not that this scenario would occur often, but I'm more curious why replace treats null and "null" as the same thing. Does replace do a .tostring on null before it does the replacement? Is this just a quirk of Javascript?

I verified this on both IE and Chrome's implementations of replace.

like image 402
petebowden Avatar asked Apr 30 '13 15:04

petebowden


People also ask

What to replace null values with?

Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them. In the example above it replaces them with 0. Cleaning data is important for analytics because messy data can lead to incorrect analysis.

How do you replace a null in a string?

There are two ways to replace NULL with blank values in SQL Server, function ISNULL(), and COALESCE(). Both functions replace the value you provide when the argument is NULL like ISNULL(column, '') will return empty String if the column value is NULL.

What is the equivalent of null in JavaScript?

In JavaScript, == compares values by performing type conversion. Both null and undefined return false. Hence, null and undefined are considered equal.

IS null == null in JavaScript?

null is a special value in JavaScript that represents a missing object. The strict equality operator determines whether a variable is null: variable === null . typoef operator is useful to determine the type of a variable (number, string, boolean).


2 Answers

Yes, this is expected according to the spec for replace (bolded relevant line, or page 146 of the ECMA-262 final draft). The first argument is checked to see if it is a regex and if not, it has toString() called on it (well, converted to a string somehow).

15.5.4.11 String.prototype.replace(searchValue, replaceValue)

Let string denote the result of converting the this value to a string.

Cut for brevity

IfsearchValue is not a regular expression, let searchString be ToString(searchValue) and search string for the first occurrence of searchString. Let m be 0.

Cut for brevity

like image 58
Russ Cam Avatar answered Oct 23 '22 01:10

Russ Cam


In the ES5 specification for String.prototype.replace:

15.5.4.11 String.prototype.replace (searchValue, replaceValue)

...

If searchValue is not a regular expression, let searchString be ToString(searchValue) and search string for the first occurrence of searchString

So, "".replace(null, XXX) will indeed convert the null to the string "null"

Note that ToString() does not mean null.toString() - it's an internal defined operation within the JavaScript interpreter.

like image 36
Alnitak Avatar answered Oct 23 '22 02:10

Alnitak