JavaScript Symbol.search Property - JavaScript Tutorial

JavaScript Symbol.search Property

BLUF: This tutorial on JavaScript Symbol.search Property provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Symbol.search Property

Understanding JavaScript Symbol.search Property is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The JavaScript Symbol.search symbol identifies the method responsible for returning the position of the first match of a regular expression within a string.

Syntax

Example

[Symbol.search](string)

Parameters

String.

Return value

The position of a string.

Browser Support

Chrome 32
Safari 8
Firefox 29
Opera 19

Example 1

Example

<script>

//JavaScript to illustrate Symbol.search

class S {  

constructor(value) 

{

    this.value = value;

  }

  [Symbol.search](string) {

return string.indexOf(this.value);

  }

}

document.write('Example'.search(new S('int')));

//expected output: 7

</script>

Output:

Example 2

Example

<script>

//JavaScript to illustrate Symbol.search

class S {  

constructor(value) 

{

    this.value = value;

  }

  [Symbol.search](string) {

return string.indexOf(this.value);

  }

}

document.write('Symbol'.search(new S('ol')));

//expected output:4

</script>

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience