Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript String with Quotes

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 06 Jan, 2023
Improve Article
Save Article

Strings are the sequence of characters, symbols, and numbers. In JavaScript, the user can write strings either in ‘single quote‘ or in “double quote“. However, both print the same result on the terminal.

'GeeksforGeeks' === "GeeksforGeeks"

In some cases, users may need to print some part of the string with the quotes. For example, I’m, Hello “Geeks”, or It’s an apple, etc. In this situation, users have to use the single quote and double quote carefully. In this tutorial, the user will learn to write the string with quotation marks.

How simple string work in JavaScript?

As mentioned in the above section, the user gets the same output whether it writes the string in a single quote or double quote. Users can see the example code below.

Example: This example prints a simple string in the console.

Javascript




<script>
    // String with double quotes
    var string1 = "Hello Geeks!!!"
      
    // String with single quotes
    var string2 = 'You are on GeeksforGeeks!'
      
    console.log(string1)
    console.log(string2)
</script>


Output:

Hello Geeks!!!
You are on GeeksforGeeks!

How user can add the quotes inside the string?

There are several ways to write a string with a quotation mark. 

Approach 1: One can use the backslash (\) inside the string to escape from the quotation mark. They will need to follow the below example to follow this method.

Syntax:

var string = "I\'m user of GeeksForGeeks."

 Example: In this example, a string with a single quote is printed in the console.

Javascript




<script>
    var string1 = "I\'m user of GeeksForGeeks."
    var string2 = 'I\'m user of GeeksForGeeks.'
    var string3 = 'Hello \'Geeks\''
    var string4 = "Hello \'Geeks\'"
      
    console.log(string1)
    console.log(string2)
    console.log(string3)
    console.log(string4)
</script>


Output:

I'm user of GeeksForGeeks.
I'm user of GeeksForGeeks.
Hello 'Geeks'
Hello 'Geeks'

Approach 2: One can write strings using alternative quotes. Users need to use single quotes inside the string if they have written the string in double quotes and vice-versa.

Syntax:

var string1 = "Hello 'Geeks'"
var strign2 = 'Hello "Geeks"'

Example: This example uses the above-explained approach to print a string with quotes in the console.

Javascript




<script>
    var string1 = "I'm user of GeeksForGeeks."
    var string2 = 'I"m user of GeeksForGeeks.'
    var string3 = 'Hello "Geeks"'
    var string4 = "Hello 'Geeks'"
      
    console.log(string1)
    console.log(string2)
    console.log(string3)
    console.log(string4)
</script>


Output:

I'm user of GeeksForGeeks."
I"m user of GeeksForGeeks.'
Hello "Geeks"
Hello 'Geeks'

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!