Justen  Hintz

Justen Hintz


String endsWith() Method in JavaScript

Originally published at https://www.samanthaming.com

Making a language more human-readable is definitely the way to go

const workout = '🏃 🏋️ 💪';
// Old Way

workout.indexOf(‘💪’, workout.length - ‘💪’.length) !== -1;
// true

// ✅ES6 Way


// true


The endsWith method accepts 2 parameters:

  1. Search Value
  2. Length

1. Search Value

This is a required field. Here is where you pass your search value. This can be a single character or longer. Let’s see some examples

Single Character

const name = ‘Samantha Ming’;

name.endsWith(‘g’); // true
name.endsWith(‘n’); // false

Multiple Characters

const name = ‘Samantha Ming’;

name.endsWith(‘ing’); // true
name.endsWith(‘in’); // false

Multiple Words

const name = ‘Samantha Ming’;

name.endsWith(‘tha Ming’); // true
name.endsWith(‘tha M’); // false

2. Length

Here you can specify the length of the string you want it to search. When I first read this, I was quite confused. So let’s try to understand what length is doing here.

First, I want to let you know the length of my first name.

console.log(‘Samantha’.length); // 8
// The last character is ‘a’

Now let’s utilize the length parameter.

const name = ‘Samantha Ming’;

name.endsWith(‘a’, 8); // true

☝️So when I say 8, I’m telling endsWith to grab the first “8” characters of the name string (from left to right). In this case, the first 8 characters of the string. And that will be the string we’re searching with endsWith.

The above example is similar to doing this:

const name = ‘Samantha’;

name.endsWith(‘a’); // true

The reason I said I was confused at first is because I pulled over my knowledge of startsWith, where the second parameter is the starting index. So I assumed the second parameter with endsWith would follow the same pattern and it would be the reverse ending index 😅 That’s why people always say, never “assume”. Just when you think you know, you actually don’t. So be humble and always keep a student mindset

If you’re interested, here’s my code notes to startsWith: Code Notes: startsWith

Case Sensitive

Here’s a knowledge that is similar to startsWith. The endsWith method is also case sensitive.

const name = ‘Samantha Ming’;

name.endsWith(‘G’); // false
name.endsWith(‘g’); // true

Browser Support

Browser support is wonderful! IF you don’t count Internet Explorer (Sorry folks, but I’m sure there is no major shock there)

Browser Support: endsWith

But no problem here’s the polyfill so you can safely use endsWith and still, have it be supported in IE.

MDN Polyfill: endsWith

By the way, if you’re a bit confused about what Polyfill is. Here’s a really good video by Tyler McGinnis.

Compiling vs Polyfills with Babel (JavaScript)

Community Input

What other old ways do you know?

Using Regex

@maxstalker: I could advocate for old way with some curried helper functions with some sprinkle of regexp

const startsWith = pattern => string =>
 new RegExp(^${pattern}.*).test(string);

const endsWith = pattern => string => new RegExp(.*${pattern}$).test(string);

const sports = “🏈🎳⛳⛸”;
console.log(startsWith(“🏈”)(sports)); // true
console.log(startsWith(“⛸”)(sports)); // false

console.log(endsWith(“🏈”)(sports)); // false
console.log(endsWith(“⛸”)(sports)); // true

@maxstalker: Or slightly shorter version, which you can inline whenever needed:

const sports = “🏈🎳⛳⛸”;

// startsWith
console.log(/^🎳./.test(sports)); // false
/.test(sports)); // true

// endsWith
console.log(/.*🎳$/.test(sports)); // false
console.log(/.*⛸$/.test(sports)); // true

Using slice

@maxstalker: Another - a bit convoluted and maybe more theoretical - way to do it would be to compare parts of the strings:

const sports = “⛳🎳🏈⚽🎾”;
const startPattern = “⛳”;
const endPattern = “⚽🎾”;

console.log(sports.slice(0, startPattern.length) === “wrong!”); // false
console.log(sports.slice(0, startPattern.length) === startPattern); // true

// endsWith
console.log(sports.slice(-endPattern.length) === “wrong!”); // false
console.log(sports.slice(-endPattern.length) === endPattern); // true

Using lastIndexOf

const workout = ‘🏃 🏋️ 💪’;

workout.lastIndexOf(‘💪’) === workout.length - ‘💪’.length;
// true

Thanks for reading

If you liked this post, please do share/like it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about JavaScript

The Complete JavaScript Course 2019: Build Real Projects!

Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

JavaScript Bootcamp - Build Real World Applications

The Web Developer Bootcamp

JavaScript Programming Tutorial - Full JavaScript Course for Beginners

New ES2019 Features Every JavaScript Developer Should Know

Best JavaScript Frameworks, Libraries and Tools to Use in 2019

React vs Angular vs Vue.js by Example

Microfrontends — Connecting JavaScript frameworks together (React, Angular, Vue etc)

#javascript #es6

What is GEEK

Buddha Community

String endsWith() Method in JavaScript
Lowa Alice

Lowa Alice


JavaScript Strings Tutorial

JavaScript Strings

📺 The video in this post was made by Programming with Mosh
The origin of the article: https://www.youtube.com/watch?v=09BwruU4kiY&list=PLTjRvDozrdlxEIuOBZkMAK5uiqp8rHUax&index=6
🔥 If you’re a beginner. I believe the article below will be useful to you ☞ What You Should Know Before Investing in Cryptocurrency - For Beginner
⭐ ⭐ ⭐The project is of interest to the community. Join to Get free ‘GEEK coin’ (GEEKCASH coin)!
☞ **-----CLICK HERE-----**⭐ ⭐ ⭐
Thanks for visiting and watching! Please don’t forget to leave a like, comment and share!

#javascript #strings #javascript strings #javascript strings tutorial

Condo Mark

Condo Mark


Top 10 JavaScript String Methods You Should Know


String methods in JavaScript help you to work with strings, mastering those methods is a good idea, because a lot of times you will find yourself working with strings in your JavaScript program or application, So you will need to know those methods. That’s why in this article I decided to show you 10 useful string methods that maybe you didn’t know some of them.

Javascript Code.

Photo by Irvan Smith on Unsplash

1. String Length

The **length** property returns the length of a string. Take a look at the example below:

Length Method.

The Length method.

2. Searching for a String

You can search for a string inside another string using the search method, It will return the position of that string. Have a look at the example below:

The Search Method in Javascript.

The Search method.

#javascript #web-development #javascript-string #javascript-tips

Madilyn  Kihn

Madilyn Kihn


Convert String To Array Using Javascript Split Method

The easiest approach to use javascript built-in method String.split().

#javascript #javascript string #string to array #morioh #array

Rahul Jangid


What is JavaScript - Stackfindover - Blog

Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.

Who invented JavaScript?

JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.

What is JavaScript?

JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.

Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.

JavaScript Hello World Program

In JavaScript, ‘document.write‘ is used to represent a string on a browser.

<script type="text/javascript">
	document.write("Hello World!");

How to comment JavaScript code?

  • For single line comment in JavaScript we have to use // (double slashes)
  • For multiple line comments we have to use / * – – * /
<script type="text/javascript">

//single line comment

/* document.write("Hello"); */


Advantages and Disadvantages of JavaScript

#javascript #javascript code #javascript hello world #what is javascript #who invented javascript

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

It is said that a digital resource a business has must be interactive in nature, so the website or the business app should be interactive. How do you make the app interactive? With the use of JavaScript.

Does your business need an interactive website or app?

Hire Dedicated JavaScript Developer from WebClues Infotech as the developer we offer is highly skilled and expert in what they do. Our developers are collaborative in nature and work with complete transparency with the customers.

The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.

Get your business app with JavaScript

For more inquiry click here https://bit.ly/31eZyDZ

Book Free Interview: https://bit.ly/3dDShFg

#hire dedicated javascript developers #hire javascript developers #top javascript developers for hire #hire javascript developer #hire a freelancer for javascript developer #hire the best javascript developers