In JavaScript, when you add a string and a number using the + operator, JavaScript performs a type conversion, and the result is a string. The number is converted to a string, and then the two strings are concatenated.
Here's an example:
let number = 5;
let string = "Hello";
let result = string + number;
console.log(result); // Output: "Hello5"
In this example, the number 5 is implicitly converted to a string, and then it's concatenated with the string "Hello," resulting in the string "Hello5."
It's important to note that JavaScript's dynamic typing allows you to perform operations like this, but you should be cautious about type conversions, especially when dealing with user input or data from external sources, to ensure that your code behaves as expected.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In JavaScript, when you add a string and a number using the + operator, JavaScript performs a type conversion, and the result is a string. The number is converted to a string, and then the two strings are concatenated.
Here's an example:
In this example, the number 5 is implicitly converted to a string, and then it's concatenated with the string "Hello," resulting in the string "Hello5."
It's important to note that JavaScript's dynamic typing allows you to perform operations like this, but you should be cautious about type conversions, especially when dealing with user input or data from external sources, to ensure that your code behaves as expected.