What is anchor() in JS?

Overview

The anchor method is defined in the String class in JS and encloses the String instance on which it is invoked with the anchor tag (e.g. <a>string_here</a>).

Syntax

anchor(name);

Parameters

  • name: a string that is to be set as the name attribute for the anchor tag.

Return value

  • A string that contains the anchor tag is returned, enclosing the string data on which it is called upon.
  • The name attribute of the anchor tag is also set, as specified by the argument name.

Example

let str = 'Hello World';
console.log(str.anchor('hello'));

In the example above, the anchor method is called on an instance of String, the str variable. The method call results in the text stored in str to be enclosed in the <a> tag. Additionally, the name argument has the value hello, so the name attribute of the anchor tag is also set to hello.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved