0

string + number

const a = "5";
const b = 7;

console.log(a + b);

Output is - 57


string * number

const a = "5";
const b = 7;

console.log(a * b);

Output is - 35

SJMormo
  • 25
  • 4
  • `+` in JavaScript can mean multiple things when used with two operands - addition, or concatenation. String concatenation is used if one of the operands is a string. Whereas for `*`, this doesn't have two meanings as `+` does, it simply just means try and multiply, regardless of the types – Nick Parsons Feb 14 '22 at 05:24
  • Relevant specification: [ApplyStringOrNumericBinaryOperator](//tc39.es/ecma262/#sec-applystringornumericbinaryoperator). – Sebastian Simon Feb 14 '22 at 05:33

1 Answers1

0

it's based on what the creators of JS thought would be more useful. In every other language you can concatenate the strings, numbers and other stuff using the +. So in JS they thought that would be a nice solution as well. But then... what does it mean to multiply a string? The creators of JS had no answer, so assumed this must be a mathematical operation.

Kamil Janowski
  • 1,872
  • 2
  • 21
  • 43