Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is anchor() in JS?

Hammad Nasir

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.

RELATED TAGS

javascript

CONTRIBUTOR

Hammad Nasir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring