One of the purposes of the +
sign in JS is to parse the right part into the number.
const str = '4';
console.log(str + 5); // Concatenared as strings
console.log(+str + 5); // Sums the numbers
In your case you have an statement i < +num
, which just parses num
into number and i
compares with it. If your num
is a number, this will do nothing.
Look. I have used '10'
instead of 10
and it still works, because the given string is parsed into number.
var laugh = function(num) {
var string="";
for (var i = 0; i < +num; i++) {
string+="ha";
}
return string + "!";
};
console.log(laugh('10'));