How to get first and last character of a string using 'String.startsWith' and 'String.endsWith' in JavaScript


String starts with is an emcscript es6 feature that allows JavaScript developers to test a string if the first character of a string matches the given criteria ,

Take for instance you have a string, you want to test if it starts with characters '#'

const str = "#JavaScript"

cons strr = "I love #kotlin"

str.startWith('#')

// true

strr.endsWith('#kotlin')

// true

strr.startWith('#')

// false

str.endsWith('#kotlin')

// false
Both functions return a boolean true for a match and false if there's no match.

endsWith does what startsWith does except that it does it at the end of the string

Comments

Popular posts from this blog

What is 'this.' keyword in JavaScript

How to make a static http server in nodejs using express

How to get user's ip address in JavaScript

How to create promises in JavaScript

How to evaluate a string on a regular expression rule in JavaScript