Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How to add Strings as numbers in JavaScript

const str1 = "4", str2="3";
// Use Number wrapper object
const sumUsingNumber = Number(str1) + Number(str2);
console.log(sumUsingNumber); // 7
// Use parseInt method
const sumUsingParseInt = parseInt(str1) + parseInt(str2);
console.log(sumUsingParseInt); // 7
// Use Unary plus
const sumUsingUnaryPlus = +str1 + +str2;
console.log(sumUsingUnaryPlus); // 7
 
PREVIOUS NEXT
Tagged: #How #add #Strings #numbers #JavaScript
ADD COMMENT
Topic
Name
2+6 =