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 thehref
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.
const str = "Click here" ;
const anchorTag = str.anchor( "https://www.geeksforgeeks.org/javascript-typedarray-reverse-method/amp/" );
console.log(anchorTag); |
<a name="https://www.geeksforgeeks.org/javascript-typedarray-reverse-method/amp/">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.