Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

get last three characters of string javascript

var member = "my name is Afia";

var last3 = member.slice(-3);

alert(last3); // "fia"
Comment

javascript string get last two character

var member = "my name is Mate";

var last2 = member.slice(-2);

alert(last2); // "te"
Comment

javascript get last n characters of string

'abc'.slice(-1); // c
Comment

find the last occurrence of a character in a string javascript

str.lastIndexOf(searchValue[, fromIndex])
Comment

How to Get the Last Two Characters of a String in JavaScript

const str = 'Coding Beauty';

const last2 = str.slice(-2);
console.log(last2); // ty

// When we pass a negative number as an argument,
// slice() counts backward from the last string character to find the equivalent index.
// So passing -2 to slice() specifies a start index of str.length - 2.

const last2Again = str.slice(str.length - 2);
console.log(last2Again); // ty

// Note
// We can use substring() in place of slice() to get the first two characters of a string:

const str = 'Coding Beauty';
const last2 = str.substring(str.length - 2);
console.log(last2); // ty

// However, we have to manually calculate the start index ourselves with str.length - 2,
// which makes the code less readable.
// This is because unlike slice(), substring() uses 0 as the start index if a negative number is passed.

const str = 'Coding Beauty';

// -2 is negative, 0 used as start index
const notLast2 = str.substring(-2);

console.log(notLast2); // Coding Beauty
Comment

last five characters of string javascript

var hello = "Hello World";

var last5 = hello.slice(-5);

alert(last5); // "World"
Comment

PREVIOUS NEXT
Code Example
Javascript :: redux store 
Javascript :: express get 
Javascript :: install express generator 
Javascript :: scroll bar disappears after closing modal 
Javascript :: javascript count up timer 
Javascript :: scale an SVG gradient to your needs in react native 
Javascript :: get the location of an item in an array 
Javascript :: replace js 
Javascript :: how to copy an arry react 
Javascript :: mongoosejs 
Javascript :: sort array without mutating js 
Javascript :: javascript swap images on mouseover 
Javascript :: use localstorage hook 
Javascript :: redux saga fetch data 
Javascript :: js url pathname 
Javascript :: signed and unsigned integers in JavaScript 
Javascript :: libuv nodejs 
Javascript :: invariant failed you should not use link outside a router test 
Javascript :: disable zoom in app 
Javascript :: reverse a string javascript 
Javascript :: trim string in javascript 
Javascript :: how to swap two images in javascript 
Javascript :: how sum all array element with for 
Javascript :: javascript append list 
Javascript :: How to end a session in ExpressJS 
Javascript :: run javascript in iframe 
Javascript :: js remove escape characters from json 
Javascript :: react function 
Javascript :: run for loop every second js 
Javascript :: javascript arreglos 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =