Passing null as a parameter to replace () in javascript behaving strangely?

I have the following jQuery:

        $("#textArea").keyup(function(){
            var textAreaValue = $("textArea");
            if(!textArea.value.indexOf("some string")){
                textArea.value = textArea.value.replace("some string",null);
                alert("It was there!");
            }
        });


  • Is it ok to element.value.replace("some string",null);replace "some string"with "null"like string? And if you can’t explain why?

  • I tested it with element.value.replace("some string","")and it works fine, so what will be the difference between nulland ""?

Using FireFox 3.6.3, thanks in advance.

+3
source share
3 answers

It seems that null was bound to strings in String.

Try:

"a" + null // "anull"

Although you cannot call toString () on an object null, it seems that the null object is implicitly converted to String, which gives the string "null".

String(null) // "null"
+3

"" - .

null - , non undefined...

null objects

str.replace(param1, param2), param1 param2 string -, string ( param2)... ,

var heart_type = 'images/unheart.png';
alert( heart_type.replace(".png",null));​

images/unheartnull.., null string...

.replace()

+1

String.replace , . . mdc w3schools. null, . , javascript-.

+1

Source: https://habr.com/ru/post/1750223/


All Articles