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.










share|improve this 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.


















    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.










    share|improve this 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.
















      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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.
























          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)';





          share|improve this answer



























            up vote
            3
            down vote













            There are two sets of quotation marks:



            'url('


            and



            ')'


            Not



            ' + product.img + '





            share|improve this answer



























              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)`





              share|improve this answer



























                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)';





                share|improve this answer
























                  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)';





                  share|improve this answer






















                    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)';





                    share|improve this answer












                    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)';






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 at 3:31









                    Poul Bak

                    5,43331132




                    5,43331132






















                        up vote
                        3
                        down vote













                        There are two sets of quotation marks:



                        'url('


                        and



                        ')'


                        Not



                        ' + product.img + '





                        share|improve this answer
























                          up vote
                          3
                          down vote













                          There are two sets of quotation marks:



                          'url('


                          and



                          ')'


                          Not



                          ' + product.img + '





                          share|improve this answer






















                            up vote
                            3
                            down vote










                            up vote
                            3
                            down vote









                            There are two sets of quotation marks:



                            'url('


                            and



                            ')'


                            Not



                            ' + product.img + '





                            share|improve this answer












                            There are two sets of quotation marks:



                            'url('


                            and



                            ')'


                            Not



                            ' + product.img + '






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 at 3:27









                            Nuri Tasdemir

                            7,89512544




                            7,89512544




















                                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)`





                                share|improve this answer
























                                  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)`





                                  share|improve this answer






















                                    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)`





                                    share|improve this answer












                                    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)`






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 12 at 3:34









                                    A. Lau

                                    3,31451952




                                    3,31451952













                                        Popular posts from this blog

                                        Top Tejano songwriter Luis Silva dead of heart attack at 64

                                        政党

                                        天津地下鉄3号線