Does either the single quote (') or double quote (") have a function where they will parse through the string and replace variables with values? I remember that in PHP the parsing engine will parse through the string and automatically switch out any variables with their values (I don't remember which actually has that effect off the top of my head) so you don't have to type "somestring" + aVariable
using the concatenation
operator. in what I have read through so far on http://www.tutorialspoint.com/jquery/jquery-basics.htm I haven't been able to find anything about it. Also unless I missed it the post When to use double or single quotes in JavaScript? does not directly cover this information.

- 1
- 1

- 21
- 1
- 4
-
1Duplicate here http://stackoverflow.com/questions/7027986/single-or-double-quotes-in-jquery – berkayk Dec 18 '15 at 17:28
-
javascript allows you to use either, which is convenient when you have quotes that contain other quotes (you use one type to enclose the string, and the other type within) – Erik Funkenbusch Dec 18 '15 at 17:28
-
Also duplicated here http://stackoverflow.com/questions/2373074/single-vs-double-quotes-vs – jered Dec 18 '15 at 17:28
-
On a side note I am hoping someone could provide clarification on what a callback in javascript is as well. I was unable to gain a clear understanding of what it is from the page on tutorials point I linked to above. – Josh Nabours Dec 18 '15 at 17:30
-
i didn't find those. probably because i didn't search using the full word. – Josh Nabours Dec 18 '15 at 17:31
-
Actually, the answer is 'no'. Duplicates are not constructive. – Gino Pane Dec 18 '15 at 17:45
-
Question re-opened: it has nothing to do with whether to use single or double quotes... – Niet the Dark Absol Dec 18 '15 at 17:45
-
@NiettheDarkAbsol those dudes just didn't read the whole question. – Gino Pane Dec 18 '15 at 17:46
-
When I had first written it it was very much a single and double quote question. I was not clear about my main issue though, so I rewrote it. – Josh Nabours Dec 18 '15 at 20:50
2 Answers
Single quotes and double quotes are identical in JavaScript and do not interpolate variables. In my experience it's good practise to stick with single quotes in JS, allowing you to use double quotes inside those strings (without escaping) for things like HTML attributes.
However, ES2015 introduced "template strings" using backticks, which are somewhat like PHP's strings in that they can interpolate into a string, and are if anything more powerful because they'll actually interpolate any expression, not just plain variables:
let bar = 'bar';
let foo = `${bar}`;
let FOO = `${bar.toUpperCase()}`;

- 334,560
- 70
- 407
- 495
-
1I don't think I would consider it *"good practice"*, since in some cases the single quotes in other languages are only for characters. More rather it's just easier to type. – Spencer Wieczorek Dec 18 '15 at 17:51
-
@SpencerWieczorek it's good to be consistent, though, and I was only talking about JS, not other languages. – Alnitak Dec 18 '15 at 17:54
-
My point is rather that you can use single quotes for HTML attributes, it doesn't really matter which way you go about it. – Spencer Wieczorek Dec 18 '15 at 17:56
-
1`opinion = 'I\'m not too sure I agree with Alnitak\'s preference for strings\' contents being in single quotes...';` – Niet the Dark Absol Dec 18 '15 at 18:03
-
@NiettheDarkAbsol whichever way you do it, it's wrong for something, but most of the time in my JS code I'm outputting HTML tags (where I have a preference for double-quoted attributes) not human readable strings. – Alnitak Dec 18 '15 at 18:06
-
@Alnitak You're right, of course. However I rarely, if ever, output HTML with JS. If I have some HTML I want to use, I either have it generated server-side in PHP, or in some cases in the source itself with ` – Niet the Dark Absol Dec 18 '15 at 18:07
-
@NiettheDarkAbsol often times you need to create HTML elements dynamically (if not using something like Angular to do it for you) – Alnitak Dec 18 '15 at 18:10
No, there is no such thing as variable interpolation in JavaScript (and therefore jQuery)
And that's a very good thing! While PHP has all variables identified by $
at the start, JavaScript does not. So even a simple string such as "Hello world!"
could go horribly wrong if you had a variable called world
...
You may be interested in a templating system, of which there are many options out there - a quick Google search will turn up results, but here's a list of some with examples and stuff.

- 320,036
- 81
- 464
- 592
-
-
and this is precisely why JS template strings explicitly use `${}` to mark the bits to be interpolated. – Alnitak Dec 18 '15 at 17:49