Nerd For Tech
Published in

Nerd For Tech

Basics of Javascript · String · endsWith() (method)

This article is a transcript of my free youtube series about basics of web development. If you prefer watching over reading, feel free to visit my channel “Dev Newbs”.

Hello my fellow newbs. Jacob here, with yet another String method. We are going on this chronologically and today’s method is called endsWith(). Let’s get to it.

The endsWith() method determines whether a string ends with the characters of a specified string. If the string ends with the characters, the method returns true, if not, it returns false. We can optionally provide also the length of the string to search in. It’s also important to mention that the method is case sensitive.

Let’s see what is the basic use case for this method in the example 1.

let str = "Welcome to the channel Dev Newbs!";str.length                                        // 33str.endsWith("!")                                 // true
str.endsWith("Dev Newbs!") // true
str.endsWith("!", 33) // true
str.endsWith("Newbs!", 33) // true
str.endsWith("?", 33) // false
str.endsWith("newbs!", 33) // false

In this example, we have a string with a sentence in a variable. Its length is equal to 33. The first block shows the cases without specified optional argument length. This means that the default length of the variable is used. It is the same as if we wrote 33 as the second argument. Second block of code with the provided second argument confirms that. So far all the results are positive.

In the third block of code, we can see that our sentence neither ends with a question mark, nor it ends with lowercase word “newbs!”. These are both false.

But we are not limited only to the alphabet, numbers and white space characters. We can also use emojis and any other special symbol as we like. There is a sentence with smiley emoji in the second example that shows how it works. Let’s have a look.

let str2 = "My name is Jacob...🙂";str2.length                                // 21str2.endsWith("🙂")                       // true
str2.endsWith("...🙂", 33) // true

Again, I output the length of the sentence first, so it is obvious what is going on in the block of code below. As you can see, emojis don’t cause any problems and are searchable as any other character.

In the next example, I would like to focus again on the optional length parameter and possibilities it provides for us. Keep in mind that the whole time, we are dealing with code units, not code points. That means it is possible to search using only part of the emoji or other symbol that consists of more than one code unit.

let str3 = "Oh, I forgot, what to do next! 🤦🤦🤦";str3.length                                       // 37
"🤦".length // 2
// how we got code unit values for facepalm emoji
"🤦".charCodeAt(0).toString(16) // d83e
"🤦".charCodeAt(1).toString(16) // dd26
// adding '\u' as prefix specifies that the value is
// hexadecimal code unit value of unicode character '\ud83e\udd26'
// searched string "Oh, I forgot, what to do next! 🤦🤦"
str3.endsWith("🤦🤦", 35) // true
// searched string "Oh, I forgot, what to do next! 🤦"
str3.endsWith("🤦", 33) // true
// searched string "Oh, I forgot, what to do next"
str3.endsWith("next!", 30) // true
// searched string "Oh, I forgot, what to do next! 🤦"
str3.endsWith("🤦🤦", 33) // false
// searched string "Oh, I forgot, what to do next! 🤦"
// "🤦" consists of two code units: \ud83e & \udd26
str3.endsWith("\udd26", 33) // true
// searched string "Oh, I forgot, what to do next! 🤦🤦"
// "🤦🤦" can be also written as \ud83e\udd26\ud83e\udd26
str3.endsWith("\udd26\ud83e\udd26", 35) // true

There is a comment line before each output explaining what part of the string is used for search. By using optional argument length, we can effectively limit the search scope based on our needs.

Explanation of how we got the unicode hex values for both code units of facepalm emoji is provided at the top of the example. It is out of scope of this method, so just take it as something that just is correct. But feel free to research it on your own, if you are interested in knowing more.

The examples themselves show us that the search and comparison is executed on the code unit level of detailness. That’s obviously good, because it allows us to do more granular comparisons.

The last example — example 4 is an attempt to simulate real-life scenario. Let’s say we have a system that logs visitors of dating site. Each visitor initiates a record being created in a log file with his or her name and a phrase “introduced himself or herself” based on a gender of the visitor. We might want to know how many males visited. Of course there are multiple other and probably better ways to do it, but let’s say we are constrained to use this method.

So let’s see the code of the last example for today.

let introductionArray = [
"Rose introduced herself",
"Tiffany introduced herself",
"Josh introduced himself",
"Sparky introduced itself",
"Andrew did not introduce himself"
];
let expectedString = "himself";introductionArray.forEach((item, index) =>{
console.log("Person '" + index + "' is male: " +
item.endsWith(expectedString));
})
// Person '0' is male: false
// Person '1' is male: false
// Person '2' is male: true
// Person '3' is male: false
// Person '4' is male: true

We have an array of records, where each item is one row from a log file and we are interested in searching males, so our search phrase is the word “himself” at the end of each item in the array.

We use a loop to go over each item in the array one by one and we check using endsWith() method whether the record is created by male visitor or not and we output the result.

Okay, so that was endsWith() and its use in more or less real-life scenarios. I think that’s about it for today.

As always, thanks a bunch for your attention and I will be looking forward to seeing you in the next article.

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

https://future-4-all.creator-spring.com/listing/save-the-date-2720

Duck Typing in Typescript for Unit Testing — using the ‘unknown’

SSL Binding in Visual Studio 2019

Mistakes to be Avoided while Developing Apps with Angular!

How to Develop a Web AR Facefilter with React and ThreeJS in 2021

Let’s get started with Algolia…

How to setup google captCHA V3

Creating a dynamic inline editable table in React — Part 1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jakub Korch

Jakub Korch

Web enthusiast, programmer, husband and a father. Wannabe entrepreneur. You name it.

More from Medium

Getting comfortable with promises in JavaScript

Back to the basics Javascript Var, Let,.

JavaScript closures and strict notation

What does a Front End developer do?

What does a Front End developer do