-2

Till now whatever programming languages I have encountered use double quotes for string and single quotes for characters but some of the javascript code that I saw seemed to be using them interchangeably,is it possible?

In which situations can they be interchanged?

In which situations they CANNOT be interchanged?

  • Not an answer, but I'll just lay it out there that it's my personal preference to always use double quotes. I'll escape (`\"...\"`) double quotes before using single quotes even crosses my mind. – ndugger Jun 10 '14 at 12:51

2 Answers2

0

They are interchangeable, but when you open a string with one, you must close it with the same character. For instance,

"Hello' would not be a valid string.

EDIT: PS, this allows for you to enclose quotes in a string. For example, if you want to have the string literal "Hello", you could have

var string = '"Hello"'
Carlos Rodriguez
  • 2,190
  • 2
  • 18
  • 29
0

yes they can as long as you keep in mind a few main things;

1- double quotes are the preferred by the browser to se they parameters values, so you never use them when setting javascript values or function calls within the DOM. 2- double quotes are also used to specify json an properties a json string using single quotes to delimiter they key, value pairs is not valid. 3-they don't complement each other, so what one starts one must end. 4 - if you need to use the same quote inside your string you need to scape it.

Dayan Moreno Leon
  • 5,357
  • 2
  • 22
  • 24