+ 3

Pls I need an explanation to this code..

Var str = "js css html php"; Var res1 = str.substring(-2,2); Var res2 = str.substr(-3,3); Var res3 = str.slice(-3); Console.log(res1 + " " + res2 + " " + res3) Answer= js php php How?? What I did.. res1= css html ph res 2= l p res3 = Js css html p

28th Mar 2022, 9:37 PM
F M
2 Respuestas
+ 3
Ipang Thanks so much
29th Mar 2022, 3:47 AM
F M
+ 2
`substring` method and `substr` method works in different ways, and so does `slice` method. Output in console: "js php php" + About substring method <res1> is result from `substring` method, and - as quoted from https://www.w3schools.com/jsref/jsref_substring.asp where it says ... "If either "start" or "end" is less than 0, it is treated as if it were 0." + About `substr` method https://www.w3schools.com/jsref/jsref_substr.asp + About `slice` method https://www.w3schools.com/jsref/jsref_slice_string.asp
28th Mar 2022, 10:05 PM
Ipang