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

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 :: fibonacci sequence array 
Javascript :: add line break in innerhtml 
Javascript :: how to make a string in javascript 
Javascript :: javascript get date value from input 
Javascript :: loop on each character js 
Javascript :: comentar en javascript 
Javascript :: how to add google map in react js 
Javascript :: if else javascript 
Javascript :: google places API details JS 
Javascript :: javascript no decimal places 
Javascript :: javascript load content from file 
Javascript :: multer 
Javascript :: how to do division in javascript 
Javascript :: map && arrow function in javascript 
Javascript :: Material-ui account circle icon 
Javascript :: javascript.loop 
Javascript :: javascript function expression 
Javascript :: js get array object from local storage 
Javascript :: how to include bootstrap in react 
Javascript :: javascript array length 
Javascript :: disable a function javascript 
Javascript :: how to display message in javascript 
Javascript :: the event object 
Javascript :: min in array 
Javascript :: for of loop in javascript 
Javascript :: javascript prompt on window close 
Javascript :: dark mode javascript 
Javascript :: pass ? url data 
Javascript :: Beep sound Javascript 
Javascript :: javascript check number length 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =