0

The following is javascript code and I'm using mozilla browser while hosting a localhost:

var test = 1; 
    console.log ("results are as such : "+ test-1 +". What we got instead is: "+ test+2 +". Astonishing!");
//The results: ("NaN. What we got instead is: 12. Astonishing!")

Doing a "Console.WriteLine("results are as such : "+ test-1 +". What we got instead is: "+ test+2 +". Astonishing!"); Will do exactly what I want it to in C# (Showing the text in quotation marks, and then showing the results of 1-1 =0, and 1+2 = 3), I'm less familiar with how it works in JavaScript, and I can't find any answers while browsing.

Can I get a hand please?

VoodooJazz_
  • 47
  • 1
  • 7
  • 1
    TLDR: `"test" - 1` is `NaN`. Not sure why C# has such a crazy operator prescedence there. – Jonas Wilms Nov 14 '21 at 23:22
  • 2
    `... +(test-1)+ ...` – Niet the Dark Absol Nov 14 '21 at 23:22
  • 1
    You need to put brackets around your mathematical expressions. Otherwise it does operations on strings and numbers (in your case, stuff like some stirng minus 1), leading to weird results such as NaN. So do like this: `console.log ("results are as such : "+(test-1)+". What we got instead is: "+(test+2)+". Astonishing!");` – Jayce444 Nov 14 '21 at 23:23

1 Answers1

1
console.log(`result are as such: ${test} - 1 = ${test - 1}. and ${test} + 2 = ${test + 2}`);
gilamran
  • 7,090
  • 4
  • 31
  • 50