1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AlphabetAdvent Calendar 2022

Day 5

【E】ECMAScript2022で追加された"String.at()"は負の数が使える

Last updated at Posted at 2022-12-04

「後ろからn文字目」が少し簡単にできる様になった

今まで

var alphabet = "abcdefghijklmnopqrstuvwxyz";
var n = 3;
/* alphabetの後ろからn文字目が知りたい */
console.log(alphabet.charAt(alphabet.length - n));
// >>> x

String.at()を使う

var alphabet = "abcdefghijklmnopqrstuvwxyz";
var n = 3;
/* alphabetの後ろからn文字目が知りたい */
console.log(alphabet.at(-1 * n));
// >>> x

.atは負の数を指定して後ろからn文字目ができます。それだけ。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?