totn JavaScript

JavaScript: String fontsize() method

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

Description

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

string.fontsize(size_value);

Parameters or Arguments

size_value
It is the font size that will be used as the size attribute for the <font> element. It can be expressed as either a numeric or relative value. Numeric values range from 1 to 7 (1 is the smallest, 7 is the largest, 3 is the default). Relative values can be values such as +1 or -2, increasing by one font size or decreasing by 2 font sizes, respectively.

Returns

The fontsize() method returns a string containing a <font> element. The value of the size_value parameter is used as the size attribute for the <font> element.

Note

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

Example

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

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.fontsize(7));

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

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

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

<font size="7">TechOnTheNet</font>

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

Specifying a Relative Size

You can also use a relative font size as a parameter for the fontsize() method.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.fontsize('+1'));

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

<font size="+1">TechOnTheNet</font>

In this example, the relative font size of '+1' is used as the size attribute for the <font> element.