Javascript escape single quote function parameter | Example code

Use ‘escape sequence’. Place a backslash \ before a single quote to escape a single quote function parameter in JavaScript.

<script>
    alert('EyeHunt\'s Answer');
</script>

Without backslash it will throw error:

Uncaught SyntaxError: missing ) after argument list

Passing a parameter to function with a single quote example

HTML example code, escape quotes/characters by prepending \ to it. A simple function to demonstrate how to work with string quotes in function and show output in console log.

<!DOCTYPE html>
<html>
<body>

  <script>
    var str1 = 'my string with "double quotes" and \'single quotes\'';
    var str2 = "my string with 'single quotes' and \"double quotes\"";

    function show(str) {
      console.log(str)
    }
    show(str1);
    show(str2)
  </script>
</body>
</html>

Output:

Javascript escape single quote function parameter

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.