If I do var a=3 and var b=4 and then do var c=a+b it tells me the answer is 34. Why?

The plus (+) is an operator that can be used for strings (e.g. letters) as well as numbers. The operator will first assume you want var a and var b work as letters not number. That means it just attaches them to 34. To make a mathematical equation out of it you have to tell the program that var a and var b are numbers by doing int(a) and int(b) while int stand for "integer".

MF
Answered by Milena F. Javascript tutor

749 Views

See similar Javascript Mentoring tutors

Related Javascript Mentoring answers

All answers ▸

Find the sum of all multiples of 2 less than 10000


What is the difference between let and var when declaring variables in javascript?


How to check if value exists in an object in JavaScript?


Suppose that you have a <p> element with an id of 'id1'. Use javascript to set the inner html of this element to 'hello!'


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences