math - Javascript (+) sign concatenates instead of giving sum of variables -


why when use this: (assuming i = 1)

divid = "question-" + i+1; 

i question-11 , not question-2?

use instead:

var divid = "question-" + (i+1) 

it's common problem , doesn't happen in javascript. idea + can represent both concatenation , addition.

since + operator handled left-to-right decisions in code this:

  • "question-" + i: since "question-" string, we'll concatenation, resulting in "question-1"
  • "question-1" + 1: since "queston-1" string, we'll concatenation, resulting in "question-11".

with "queston-" + (i+1) it's different:

  • since (i+1) in parenthesis, value must calculated before first + can applied:
    • i numeric, 1 numeric, we'll addition, resulting in 2
  • "question-" + 2: since "question-" string, we'll concatenation, resulting in "question-2".

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -