-3

What is the difference between 'xxx' and xxx? For example in below code:

namespace = 'api';
...
return `${super.buildURL(...args)}.json`;
lei lei
  • 1,753
  • 4
  • 19
  • 44
  • Interpolation of expressions via `${ ... }` doesn't happen in single-quote strings. – Pointy Nov 15 '22 at 02:20
  • This is a thing you could very easily determine from any online JavaScript reference. – Pointy Nov 15 '22 at 02:21
  • 3
    To be fair, it is hard to google for an answer since punctuation like quote marks is usually ignored in the search query (except when using double quotes to surround an exact phrase match). – GregL Nov 15 '22 at 02:29

1 Answers1

1

Single quotes is just a string literal, whereas backticks allow you to refer to other variables (or functions) inline via the $ and {} or () characters.

Also newlines are interpretted differently in each type. With quote marks, you need to use \n for newlines, whereas backtick strings are WYSIWYG with regard to newlines. You can also use double quotes as well. This is useful when you want to actually use quote marks in your strings, for example:

    var my_string = '"Hello", she said.';
    console.log("mystring:" + my_string);

would produce:

mystring: "Hello", she said.

This is one of the most useful things I find about the mixing of quote marks for strings, you can use the other style of quote without having to escape it! Makes code so much more readable.

Noscere
  • 417
  • 3
  • 8