Featured Article

Master w3schools Includes Guide Learn HTML CSS JavaScript Examples

Adam Jul 13, 2026

w3schools' 'includes()' function is a powerful and often-overlooked tool for simplifying computations in HTML, CSS, and JavaScript. By allowing attribute values to be dynamically set, 'includes()' enhances the flexibility and responsiveness of web development. Let's delve into this function, its uses, and best practices.

a class room filled with desks covered in notebooks and lined up notes that read what the average school year math class is
a class room filled with desks covered in notebooks and lined up notes that read what the average school year math class is

'includes()' is a string method that checks whether one string contains another as a sequence. Understanding this can unlock its potential in web development. Whether you're an aspiring developer or a seasoned professional, grasping 'includes()' can streamline your work, making it more efficient and accurate.

a collage of photos with the words the school written on them, and an open book
a collage of photos with the words the school written on them, and an open book

Understanding 'includes()'

'includes()' is used to check if a string contains a specified sequence of characters. It returns a Boolean value: true if the string contains the specified characters, and false otherwise.

studious ๐Ÿ“’๐Ÿ“‘
studious ๐Ÿ“’๐Ÿ“‘

The syntax is: string.includes(searchValue, startPosition). Here, 'searchValue' is the value to search for, and 'startPosition' is the position at which to start the search. If 'startPosition' is not specified, the search is conducted from the start of the string.

Basic Usage

there is a collage of different items in the image, including an apple laptop
there is a collage of different items in the image, including an apple laptop

Here's a simple example: 'const text = "Hello, World!"; console.log(text.includes("World"));' This will output 'true', as "World" is part of the string "Hello, World!".

To specify a starting position, you can use 'startPosition'. For instance, 'console.log(text.includes("World", 7))' starts the search at the eighth character, so it returns 'false'.

Real-world Applications

So was 2025-2026
So was 2025-2026

In web development, 'includes()' can be used for dynamic attribute values. For example, you might have a script that generates parts of a webpage. If a certain element should only be displayed under specific conditions, you can use 'includes()' to check if the page meets those conditions.

Here's a simple HTML script that uses 'includes()':

<script>
  const username = "John";
  const usernameDisplay = document.getElementById("username");
  if (username.includes("a")) {
    usernameDisplay.innerHTML = "Welcome, " + username;
  } else {
    usernameDisplay.innerHTML = "Welcome, Guest";
  }
</script>
This script checks if the username includes the character 'a'. If it does, it greets the user; if not, it greets a 'Guest'.

'includes()' in Practice

me core
me core

One caveat of 'includes()' is its case-sensitivity. 'const text = "Hello, World!"; console.log(text.includes("world"));' returns 'false', despite "world" being part of the string. To perform case-insensitive searches, convert both the string and the search value to the same case before using 'includes()'.

Another important practice is to avoid using 'includes()' for long strings. As 'includes()' checks every character up to the search value, using it with long strings can negatively impact performance, as it involves more operations. For shorter strings or specific, defined strings, 'includes()' is both simple and effective.

the back cover of school six hour hours of our lives, written in black and white
the back cover of school six hour hours of our lives, written in black and white
an assortment of clothing and accessories including shoes, sunglasses, headbands and purse
an assortment of clothing and accessories including shoes, sunglasses, headbands and purse
the british system of education and free school are called state schools
the british system of education and free school are called state schools
w3schools includes ()
w3schools includes ()
a poster with the words yes, yes and yes written in different colors on it
a poster with the words yes, yes and yes written in different colors on it
WEAK HERO CLASS
WEAK HERO CLASS
ahhhhhhhh
ahhhhhhhh
w3schools includes ()
w3schools includes ()
i start on thursday โ˜น๏ธ
i start on thursday โ˜น๏ธ
Uk High School
Uk High School
whispers #Whispers #studying #school #parisgeller #RoryGilmore
whispers #Whispers #studying #school #parisgeller #RoryGilmore
All this for zero work done in the end
All this for zero work done in the end
Whisper
Whisper
F4 Thailand School Uniform, Xo Kitty School Uniform, Maxton Hall School Uniform, University Uniform, Dream Academy Uniform Sketch, Titan Academy Uniform, School Uniform With Blazer, Blazer Uniform School
F4 Thailand School Uniform, Xo Kitty School Uniform, Maxton Hall School Uniform, University Uniform, Dream Academy Uniform Sketch, Titan Academy Uniform, School Uniform With Blazer, Blazer Uniform School
a girl is making the peace sign in front of her class room desks with other students
a girl is making the peace sign in front of her class room desks with other students
British school vibes ๐Ÿ‡ฌ๐Ÿ‡ง
British school vibes ๐Ÿ‡ฌ๐Ÿ‡ง
High School
High School
like wdym theres only two weeks until school startsโ‰๏ธ๐Ÿ˜จ
like wdym theres only two weeks until school startsโ‰๏ธ๐Ÿ˜จ

Alternatives to 'includes()'

While 'includes()' is powerful, it's not always the best tool for the job. For longer, more complex strings, consider using 'indexOf()' instead. 'indexOf()' returns the position of the first occurrence of the given value, or -1 if it doesn't exist. Being able to return a specific index allows for more flexibility.

However, 'indexOf()' also has its drawbacks. It's not as readable as 'includes()', and it only checks for the first occurrence, not all instances of the search value. Knowing when to use 'includes()' and when to use 'indexOf()' is a fundamental part of mastering JavaScript.

'includes()' is a versatile tool in the web developer's arsenal. By understanding 'includes()' - when and how to use it - you can write cleaner, more efficient code. Whether you're a beginner or an experienced professional, deepening your understanding of 'includes()' can help you reach the next level in web development.