どうも、ちょげ(@chogetarou)です。
slice()を使って文字列(string)を最後の文字を取得する方法を紹介します。
方法

slice()を使って文字列(string)を最後の文字を取得するには、負の値を使います。
まず、文字列からslice()を呼び出します。
そして、slice()の引数に「−1」を指定します。
const last = text.slice(-1);
上記のslice()は、呼び出した文字列の最後の文字を取得します。
使用例
const text = "Hello,World";
const last = text.slice(-1);
console.log(text);
console.log(last);
出力:
Hello,World
d
コメント