Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ' and " in JavaScript?

I saw this question and I am wondering about the same thing in JavaScript.

If you use the character ' or the character " when making strings in JavaScript, the application seems to behave the same. So what is the difference between these two characters?

The only advantage I have seen in using ' to build strings is that I can do stuff like:

var toAppend = '<div id="myDiv1"></div>'; 

Instead of:

var toAppend = "<div id=\"myDiv1\"></div>"; 

Is there any significant difference between them that I should be aware of?

like image 832
fmsf Avatar asked Jun 03 '09 10:06

fmsf


People also ask

What is the difference between == and === in JavaScript with example?

= is used for assigning values to a variable in JavaScript. == is used for comparison between two variables irrespective of the datatype of variable. === is used for comparision between two variables but this will check strict type, which means it will check datatype and compare two values.

What is difference between === and ==?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

Why do we use === in JavaScript?

=== (Triple equals) is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 2 with “2” using ===, then it will return a false value.

What is the difference between == and === in JavaScript Mcq?

== is used for the comparison between two variables regardless of the type of the variable. === is used for a strict comparison between two variables i.e. it will check the type and value of both variables, which means it will check the type and compare the two values.

What is the difference between == and === operator in JavaScript?

The ‘==’ operator tests for abstract equality i.e. it does the necessary type conversions before doing the equality comparison. But the ‘===’ operator tests for strict equality i.e it will not do the type conversion hence if the two values are not of the same type, when compared, it will return false.

What is the difference between single = and == in JavaScript?

The single = is used for assigning the value to variable and == , === are used for comparison purposes. == compares two variables irrespective of data type while === compares two variables in a strict check, which means it checks for data type also then it returns true or false.

What is the difference between JScript and JavaScript?

JScript: JScript is same of JavaScript as JScript was the variant of Microsoft’s JavaScript. JScript was named so for it implementation because Microsoft wanted to avoid trademark issues as trademark of JavaScript is Oracle Corporation. JavaScript: It is a scripting language whose trademark is Oracle Corporation.

What is == in JavaScript and how to use it?

What is == in JavaScript? Double equals (==) is a comparison operator, which transforms the operands having the same type before comparison. So, when you compare string with a number, JavaScript converts any string to a number. An empty string is always converts to zero.


2 Answers

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.

like image 181
Paolo Bergantino Avatar answered Oct 13 '22 07:10

Paolo Bergantino


Although not technically a difference in Javascript, its worth noting that single quoted strings are not valid JSON, per se. I think that people automatically assume that since JSON is valid JS, that valid JS strings are also valid JSON, which isn't necessarily true.

E.g., {'key': 'Some "value"'} is not valid JSON, whereas {"key": "Some 'value'"} is.

like image 34
B Robster Avatar answered Oct 13 '22 05:10

B Robster