Question: What is the equivalent javascript code for `mysql_real_escape_string()`?

Answer: 
A good tip when you're looking for javascript equivalent versions of popular PHP functions is

[phpjs.org](http://phpjs.org)

However your probably looking for the wrong functionality. mysql\_real\_escape\_string() can only be used if a mysql database connection is active (More details about this can be read on the [mysqli real\_escape page](http://php.net/manual/en/mysqli.real-escape-string.php). I.e mysql\_real\_escape\_string() isn't really a PHP implementation, but more MySQL. If used without database connection it will just leave you with an empty variable.

You might have looked for a function that adds slashes and removes newlines? You can check the phpjs.org on how they implemented the add\_slashes function. Removing newlines can be done with a custom replace function I guess.

Maybe explain in what environment you require this function to work in javascript, so we could push you in the right direction (i.e talk you out of using mysql\_real\_escape\_string in javascript but provide tips on which function(s) you really need)
There isn't any, because strings that come from JavaScript are still considered 'user input' and **should never be inserted in your database directly**. Your server-side has to do additional validation. There are functions to encode URLs and stuff like that. 

What are you trying to do?