Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference (if there is any) between ` and ' in javascript [duplicate]

Recently ran into some JS code that uses ` and '. I can't figure out if there is a different use for each apostrophe. Is there any?

like image 400
roscioli Avatar asked Nov 12 '15 19:11

roscioli


People also ask

What is difference between != and !== In JavaScript?

!== means that two variables are being checked for both their value and their value type ( 8!== 8 would return false while 8!== "8" returns true). !=

What is the difference && and & in JavaScript?

& is a bitwise operator and compares each operand bitwise. It is a binary AND Operator and copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100. Whereas && is a logical AND operator and operates on boolean operands.

Why do we prefer === and !== Over == and != In JavaScript?

The strict equality operator ( === ) behaves identically to the abstract equality operator ( == ) except no type conversion is done, and the types must be the same to be considered equal. The == operator will compare for equality after doing any necessary type conversions.

Which of the following is a difference between == and === in JavaScript select the most accurate answer?

Since == doesn't bother with types, that returns true. However, if you want strict type checking, you'd use === because that returns true only if the it's of the same type, and is the same value. positions. Two numbers are strictly equal when they are numerically equal (have the same number value).


1 Answers

' or " denotes a string

` denotes a template string. Template strings have some abilities that normal strings do not. Most importantly, you get interpolation:

var value = 123; console.log('test ${value}') //=> test ${value} console.log(`test ${value}`) //=> test 123 

And multiline strings:

console.log('test test') // Syntax error  console.log(`test test`) // test // test 

They have some other tricks too, more on template strings here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings

Note they are not supported in all javascript engines at the moment. Which is why template strings are often used with a transpiler like Babel. which converts the code to something that will work in any JS interpreter.

like image 129
Alex Wayne Avatar answered Sep 30 '22 15:09

Alex Wayne