+ 1

Can someone explain step by step, what is happening in this JavaScript code?

let acc=1234567891; let last= +acc.toString()[acc.toString().length-1]; switch(last){ // 1,6 case 1: case 6: console.log("eligible"); break default : console.log("not eligible"); }

5th May 2023, 1:02 PM
akhil vaid
akhil vaid - avatar
1 Resposta
+ 5
since acc is a number so acc.toString() will convert to string (as you cannot access a value from a number using index so you have to convert number in string) now the length of converted string is 10 So +acc.toString()[10 - 1]; +acc.toString()[9] +1 //here 1 is a string because of acc.toString() but there is + so +1 will be a number So last = 1 Just try this console.log(typeof (acc.toString()[acc.toString().length - 1])) console.log(typeof +(acc.toString()[acc.toString().length - 1]))
5th May 2023, 1:18 PM
A͢J
A͢J - avatar