Блог пользователя zhanglinjie_

Автор zhanglinjie_, история, 5 лет назад, По-английски

In both python and JavaScript we can access the char of string with bracket:

in python

s = "abc"
s[0]
# it returns 'a'

in JavaScript

s = "abc"
s[0]
// it returns 'a'

But if we use the index -1 to get the last char

in python

s[-1]
# it returns 'c'

in JavaScript

s[-1]
# it returns undefined

Fortunately, we can simply use s[s.length - 1].

  • Проголосовать: нравится
  • -28
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

I prefer s.slice(-1)[0] if s is a long expression.