JavaScript Symbol.replace Property - JavaScript Tutorial

JavaScript Symbol.replace Property

BLUF: This tutorial on JavaScript Symbol.replace 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.replace Property

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

The JavaScript Symbol.replace symbol is responsible for identifying the method that substitutes the matched substring within a string.

Syntax

Example

[Symbol.replace](string).

Parameters

String.

Return value

A new string.

Browser Support

Chrome 32
Safari 8
Firefox 29
Opera 19

Example 1

Example

<script>

class Rep {

  constructor(value) {

    this.value = value;

  }

  [Symbol.replace](string) {

    return `${string}`;

  }

}

var r=new Rep("java");

document.writeln("Before: "+r.value);

document.writeln("After: "+"Example".replace(r.value));

</script>

Output:

Output

Before: java

After: Example

Example 2

Example

<script>

class Rep {

  constructor(value) {

    this.value = value;

  }

  [Symbol.replace](string) {

    return `${string}`;

  }

}

var r=new Rep("Example");

document.writeln("Before: "+r.value);

document.writeln("After: "+r.value.toUpperCase().replace(r.value));

</script>

Output:

Output

Before: Example

After: Example

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