totn JavaScript

JavaScript: String link() method

This JavaScript tutorial explains how to use the string method called link() with syntax and examples.

Description

In JavaScript, link() is a string method that is used to create the HTML <a> element with a hyperlink. Because the link() method is a method of the String object, it must be invoked through a particular instance of the String class.

Syntax

In JavaScript, the syntax for the link() method is:

string.link(href_value);

Parameters or Arguments

href_value
It is the URL target for the hyperlink that will be used as the href attribute for the <a> element.

Returns

The link() method returns a string containing an <a> element. The value of string is enclosed in <a> and </a> tags as the hyperlink text and the value of the href_value parameter is used as the href attribute for the <a> element.

Note

  • The link() method does not change the value of the original string.

Example

Let's take a look at an example of how to use the link() method in JavaScript.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.link('https://www.techonthenet.com/index.php'));

In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the link() method of the totn_string variable to return a string that contains the HTML <a> element.

We have written the output of the link() method to the web browser console log, for demonstration purposes, to show what the link() method returns.

The following will be output to the web browser console log:

<a href="https://www.techonthenet.com/index.php">TechOnTheNet</a>

As you can see, the link() method created a string that contains an <a> element. The value of the totn_string variable (which is 'TechOnTheNet') is enclosed within the <a> and </a> tags. The parameter that was passed into the link() method is used as the href attribute for the <a> element.