totn JavaScript

JavaScript: String sub() method

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

Description

In JavaScript, sub() is a string method that is used to create the HTML <sub> element. Because the sub() 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 sub() method is:

string.sub();

Parameters or Arguments

There are no parameters or arguments for the sub() method.

Returns

The sub() method returns a copy of string enclosed in <sub> and </sub> tags.

Note

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

Example

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

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.sub());

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

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

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

<sub>TechOnTheNet</sub>

As you can see, the sub() method created a string that contains a <sub> element. The value of the totn_string variable (which is 'TechOnTheNet') is enclosed within the <sub> and </sub> tags.