Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript String with Quotes

  • Last Updated : 31 Oct, 2021

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 works in JavaScript?

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

Example:

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:

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 string in double quotes and vice-versa.

Syntax:

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

Example:

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
Recommended Articles
Page :

Start Your Coding Journey Now!