totn JavaScript

JavaScript: String fontcolor() method

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

Description

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

string.fontcolor(color_value);

Parameters or Arguments

color_value
It is a color name or color value that will be used as the color attribute for the <font> element.

Returns

The fontcolor() method returns a string containing a <font> element. The value of the color_value parameter is used as the color attribute for the <font> element.

Note

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

Example

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

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.fontcolor('blue'));

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

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

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

<font color="blue">TechOnTheNet</font>

As you can see, the fontcolor() 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 string value of 'blue' that was passed into the fontcolor() method as a parameter is used as the color attribute for the <font> element.

Specifying a Hexadecimal Value

You can also use a Hexadecimal color value as a parameter for the fontcolor() method.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.fontcolor('#0000FF'));

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

<font color="#0000FF">TechOnTheNet</font>

In this example, the Hexadecimal representation of the color blue (which is #0000FF) is used as the color attribute for the <font> element.