Open In App

What is anchor() Method in JavaScript ?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The anchor() method in JavaScript is a string method used to create an HTML anchor element (<a>) with the specified text (the string itself) as the visible content, and a URL specified as the method’s argument as the href attribute of the anchor element.

Syntax:

string.anchor(name)
  • string: The string to be displayed as the anchor text.
  • name: The URL to be assigned to the href attribute of the anchor element.

Example: Here, the anchor() method is called on the str string with the URL "https://example.com" as its argument. This creates an HTML anchor element (<a>) with the text "Click here" as its content and the URL "https://example.com" as its href attribute value. The resulting anchor element is then assigned to the anchorTag variable.

Javascript




const str = "Click here";
 
console.log(anchorTag);


Output

<a name="https://www.geeksforgeeks.org/javascript-typedarray-reverse-method/">Click here</a>

Note:

  • The anchor() method is rarely used in modern JavaScript programming because it directly mixes JavaScript with HTML markup, which is generally considered bad practice for code readability and maintainability.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads