[string].includes(searchValue)
In apps you sometimes need to check if one string is contained in another string; for example if a userid contains a space or not since spaces are not allowed. includes() returns a boolean true or false and is sometimes used similarly to a boolean expression in the condition of an if, if-else, or while statement. To call the includes function you need use a variable containing a string, followed by a dot ("."), followed by the function name "includes()" with a string argument or variable of what to search for.
Examples
Example: is it a question?
// See if a sentence is a question or not.
var sentence="How old are you?";
console.log(sentence.includes("?"));
Example: case sensitive
Demonstrate that includes() is case sensitive.
// Demonstrate that includes is case sensitive.
var gettysburgAddress="Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.";
console.log(gettysburgAddress.includes("Four"));
console.log(gettysburgAddress.includes("four"));
Example: movie sequels
Check if a movie is a sequel or not.
// Check if a movie is a sequel or not.
textLabel("movieLabelID", "Favorite Movie: ");
textInput("movieInputID", "");
onEvent("movieInputID", "change", function(event) {
if (getText("movieInputID").includes("2") || getText("movieInputID").includes("II")) {
write("Sequels are never as good as the original.");
} else {
write("I like that one also.");
}
});
Syntax
[string].includes(searchValue)
Parameters
Name | Type | Required? | Description |
---|---|---|---|
string | string | The string to search. | |
searchValue | string | The string to search for. |
Found a bug in the documentation? Let us know at support@code.org.