skip to Main Content

The following code doesn’t work as expected. How can I stop it from concatenating?

function blah(){
let testArray = [1,2,3,4,5];

      console.log('testArray[0]+1 = ' + testArray[0]+1);
      }

https://jsfiddle.net/ut2k67oc/

How to I get this to add an array element to another integer instead of concatenating?

3

Answers


  1. function blah() {
      let testArray = [1, 2, 3, 4, 5];
      console.log('testArray[0]+1 = ' + (testArray[0] + 1));
    }
    
    Login or Signup to reply.
  2. Use parenthesis

    function blah(){
      let testArray = [1,2,3,4,5];
      console.log('testArray[0]+1 = ' + (testArray[0]+1));
    }
    
    Login or Signup to reply.
  3. You can use the template literals

    function blah() {
      const str = 'testArray[0]+1 = ';
      const testArray = [1, 2, 3, 4, 5];
      console.log(`${str}${testArray[0] + 1}`);
    }
    blah()
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search