Both of them are completely acceptable to be used in Javascript and usage depends on personal preference. I would recommend trying to be consistant with your usage to help keep your code clean and readable (not required by any means but a personal preference).
Also, the two can be used alongside one another when you need to include string literals within your values :
//These two statements are equivalent
alert('My name is "<%= yourName %>"');
alert("My name is '<%= yourName %>");
They are equivalent for all intents and purposes. If you want to use either one inside a string, it is a good idea to use the other one to create the string, as you noted. Other than that, it's all the same.
You can use " inside ' as below,
var str='<div class="test">'
Marked as answer by ToughMan on Feb 22, 2013 06:06 AM
ToughMan
Participant
1490 Points
635 Posts
When to use single quote or double one?
Feb 22, 2013 03:02 AM|LINK
Everyone;)
Just a simple question——
If I wanna alert a messagebox in js, I can say:
alert('<%=SomeVariable defined at code-behind%>');
But if I use double quote——"" instead of ''.
This will output a whole string "<%=SomeVariable defined at code-behind%>".
Can anyone tell us when to use single quote, when double one, when we can use either of them together?
roopeshreddy
All-Star
20155 Points
3328 Posts
Re: When to use single quote or double one?
Feb 22, 2013 03:48 AM|LINK
Hi,
I didn't see any difference, and the following code yeilds me the same result -
<script type="text/javascript"> alert("<%=Name%>"); </script> //Code behind public string Name = "BillGates";Hope it helps u...
Roopesh Reddy C
Roopesh's Space
Rion William...
All-Star
27896 Points
4618 Posts
Re: When to use single quote or double one?
Feb 22, 2013 04:35 AM|LINK
Both of them are completely acceptable to be used in Javascript and usage depends on personal preference. I would recommend trying to be consistant with your usage to help keep your code clean and readable (not required by any means but a personal preference).
Also, the two can be used alongside one another when you need to include string literals within your values :
//These two statements are equivalent alert('My name is "<%= yourName %>"'); alert("My name is '<%= yourName %>");For some additonal reading on the topic, I recommend checking out this Stack Overflow discussion.
prabu.raveen...
Contributor
5022 Points
956 Posts
Re: When to use single quote or double one?
Feb 22, 2013 04:46 AM|LINK
They are equivalent for all intents and purposes. If you want to use either one inside a string, it is a good idea to use the other one to create the string, as you noted. Other than that, it's all the same.
You can use " inside ' as below,
var str='<div class="test">'