0

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" + aVariableusing 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.

Community
  • 1
  • 1
Josh Nabours
  • 21
  • 1
  • 4

2 Answers2

2

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()}`;
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • 1
    I 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
1

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.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592