Why the plus signs before the quotation marks in Javascript? [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
plus operator with quotes in javascript
3 answers
I've stumbled across something I can't quite get my head around. Let's say there's a product object:
let product =
img: "../image.jpg"
Now if I want to access product.img in a url, I can do something like the following:
const getProductImage = product => (
backgroundImage: 'url(' + product.img +')'
);
My question is: what is going on with this bit?:
' + product.img + '
EDIT: Okay, this question starts from the wrong premise, see the answer below.
javascript string
marked as duplicate by vol7ron, Community♦ Nov 12 at 4:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
This question already has an answer here:
plus operator with quotes in javascript
3 answers
I've stumbled across something I can't quite get my head around. Let's say there's a product object:
let product =
img: "../image.jpg"
Now if I want to access product.img in a url, I can do something like the following:
const getProductImage = product => (
backgroundImage: 'url(' + product.img +')'
);
My question is: what is going on with this bit?:
' + product.img + '
EDIT: Okay, this question starts from the wrong premise, see the answer below.
javascript string
marked as duplicate by vol7ron, Community♦ Nov 12 at 4:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
plus operator with quotes in javascript
3 answers
I've stumbled across something I can't quite get my head around. Let's say there's a product object:
let product =
img: "../image.jpg"
Now if I want to access product.img in a url, I can do something like the following:
const getProductImage = product => (
backgroundImage: 'url(' + product.img +')'
);
My question is: what is going on with this bit?:
' + product.img + '
EDIT: Okay, this question starts from the wrong premise, see the answer below.
javascript string
This question already has an answer here:
plus operator with quotes in javascript
3 answers
I've stumbled across something I can't quite get my head around. Let's say there's a product object:
let product =
img: "../image.jpg"
Now if I want to access product.img in a url, I can do something like the following:
const getProductImage = product => (
backgroundImage: 'url(' + product.img +')'
);
My question is: what is going on with this bit?:
' + product.img + '
EDIT: Okay, this question starts from the wrong premise, see the answer below.
This question already has an answer here:
plus operator with quotes in javascript
3 answers
javascript string
javascript string
edited Nov 12 at 3:32
asked Nov 12 at 3:19
user211309
399
399
marked as duplicate by vol7ron, Community♦ Nov 12 at 4:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by vol7ron, Community♦ Nov 12 at 4:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
Your code simply adds the strings together, so the result is the new string concatenated:
'url(' + product.img +')'.
For instance, if the product.img
contains an url to an image, like: 'http:example.com/image1.png'
then the backgroundimage
will end up containing:
'url(http:example.com/image1.png)';
add a comment |
up vote
3
down vote
There are two sets of quotation marks:
'url('
and
')'
Not
' + product.img + '
add a comment |
up vote
1
down vote
Because that's how you concat a string from variables into a single string.
It could be rewritten as follows:
let first = 'url('
let last = ')'
let final_string = first + product.img + last
Or in template literals (which is better where possible)
`url($product.img)`
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your code simply adds the strings together, so the result is the new string concatenated:
'url(' + product.img +')'.
For instance, if the product.img
contains an url to an image, like: 'http:example.com/image1.png'
then the backgroundimage
will end up containing:
'url(http:example.com/image1.png)';
add a comment |
up vote
1
down vote
accepted
Your code simply adds the strings together, so the result is the new string concatenated:
'url(' + product.img +')'.
For instance, if the product.img
contains an url to an image, like: 'http:example.com/image1.png'
then the backgroundimage
will end up containing:
'url(http:example.com/image1.png)';
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your code simply adds the strings together, so the result is the new string concatenated:
'url(' + product.img +')'.
For instance, if the product.img
contains an url to an image, like: 'http:example.com/image1.png'
then the backgroundimage
will end up containing:
'url(http:example.com/image1.png)';
Your code simply adds the strings together, so the result is the new string concatenated:
'url(' + product.img +')'.
For instance, if the product.img
contains an url to an image, like: 'http:example.com/image1.png'
then the backgroundimage
will end up containing:
'url(http:example.com/image1.png)';
answered Nov 12 at 3:31
Poul Bak
5,43331132
5,43331132
add a comment |
add a comment |
up vote
3
down vote
There are two sets of quotation marks:
'url('
and
')'
Not
' + product.img + '
add a comment |
up vote
3
down vote
There are two sets of quotation marks:
'url('
and
')'
Not
' + product.img + '
add a comment |
up vote
3
down vote
up vote
3
down vote
There are two sets of quotation marks:
'url('
and
')'
Not
' + product.img + '
There are two sets of quotation marks:
'url('
and
')'
Not
' + product.img + '
answered Nov 12 at 3:27
Nuri Tasdemir
7,89512544
7,89512544
add a comment |
add a comment |
up vote
1
down vote
Because that's how you concat a string from variables into a single string.
It could be rewritten as follows:
let first = 'url('
let last = ')'
let final_string = first + product.img + last
Or in template literals (which is better where possible)
`url($product.img)`
add a comment |
up vote
1
down vote
Because that's how you concat a string from variables into a single string.
It could be rewritten as follows:
let first = 'url('
let last = ')'
let final_string = first + product.img + last
Or in template literals (which is better where possible)
`url($product.img)`
add a comment |
up vote
1
down vote
up vote
1
down vote
Because that's how you concat a string from variables into a single string.
It could be rewritten as follows:
let first = 'url('
let last = ')'
let final_string = first + product.img + last
Or in template literals (which is better where possible)
`url($product.img)`
Because that's how you concat a string from variables into a single string.
It could be rewritten as follows:
let first = 'url('
let last = ')'
let final_string = first + product.img + last
Or in template literals (which is better where possible)
`url($product.img)`
answered Nov 12 at 3:34
A. Lau
3,31451952
3,31451952
add a comment |
add a comment |