🔥 NEW Code Tidbit Every Week 🔥

# Trick to Adding String and Number

The unary + operator is a shortcut to convert a string into a number 🤩

Problem: Adding strings that contain numbers does NOT add them. Instead, the strings are concatenated ❌

Solution: Convert the string into a number. Then you can calculate the sum ✅

const string = '100';
const number = 5;

// This doesn't return the sum, it's concatentated
console.log(string + number); // 1005

// Prepend string with "+" to calculate the sum
console.log(+string + number); // 105

# Resources


Related Tidbits