Coding / Programming Videos

Post your favorite coding videos and share them with others!

JavaScript String Contains: How to check a string exists in another

In this tutorial, you’ll learn how to use the JavaScript String Contains function to determine whether a string exists in another.

Don’t forget to subscribe to the Junior Developer Central channel for more videos and tutorials!

In a bit of a spoiler, there actually is no contains function in JavaScript but if you want to use JavaScript to check if a string contains a substring.

There are a few ways you can check if a certain substring exists in a string with JavaScript but we’ll focus on a classic, pre-ES6 way, using the indexOf function which will give you the most browser compatibility.

Using the indexOf function you check where a JavaScript string contains characters that you pass as an argument. If the string exists, you get a positive value indicating the position of the substring in the base string. If the string doesn’t exist, you receive a value of -1.

By checking the returned value of indexOf you can determine whether the string contains the substring.

This traditional method will appear in a lot of legacy code but we’ll also take a quick look at the ES6 version of this technique which is using the string includes function which operates in a similar way as indexOf but returns a boolean value.

Other videos in the JavaScript Snippets series:
Find the largest number in an array JavaScript Tutorial: https://youtu.be/fOFSmCjvcFY
How to find the longest word in a String with JavaScript: https://youtu.be/j9cAav7VIv4
How to reverse a String in JavaScript Tutorial: https://youtu.be/k7zzWvQaEhc
Javascript String Length: How to determine the size of a string: https://youtu.be/OO0_9gq8NNI
JavaScript join method: How to merge arrays into one value: https://youtu.be/90MVWda5DlM
JavaScript How To Remove An Item From Array Tutorial: https://youtu.be/UvohHcj9I-s
Javascript How To Convert String To Number Tutorial: https://youtu.be/K-Twvsg3K_M
JavaScript Create HTML Element: How to dynamically add tags to your pages: https://youtu.be/VsXCK_2DJzA
JavaScript String Contains: How to check a string exists in another: https://youtu.be/uLT9iPccVZs
Link JavaScript to HTML: How to run your JavaScript code in the browser: https://youtu.be/821C5aJ3SLM
JavaScript Copy Array: How to make an exact copy of an array in JavaScript: https://youtu.be/5rybkWfeh-A
JavaScript Capitalize First Letter: How to make strings and arrays sentence case: https://youtu.be/8IEI-7fj2j4
Javascript Print To Console Tutorial: Different ways to output data to the console: https://youtu.be/pkFbigsR7jI

Bookmark(0)
 

Leave a Reply

Please Login to comment
  Subscribe  
Notify of
Translate »