How to make a text italic using JavaScript

In JavaScript, the italics function is utilized to render a string in italics. This transforms the text to resemble how it would appear within the <i> </i> (italic tag). As the italics function belongs to the string object, it needs to be called on a particular instance of the string class.

Its role is to apply italic formatting to a string. This function does not modify the original string's value; rather, it generates and returns a new string that is italicized.

Typically, this approach yields the string formatted in the manner outlined below. For instance, if we take the string "Hello world!" and utilize the italics method on it, subsequently displaying the result with the alert function, the output will be -

Example

<i>Hello world!</i>

The resulting output will be a string enclosed within the <i> </i> tag.

Syntax

Example

string.italics();

This function does not necessitate any parameters. Now, let’s explore the application of the italics method through several examples.

Example1

In this instance, we are applying italicization to a string. In this context, we have a string variable named str, and we utilize the italicize method to render the characters of the string in italics.

To view the output, it is necessary to click the provided HTML button. In this instance, we are utilizing the document.write method to display the result.

Example

<html>

<head>



</head>

<body>

<h1> Hello World :) :) </h1>

<p> Click the following button to see the effect. </p>

<p id = "para"> </p>

<button onclick = "fun()"> Click me </button>

<script>

function fun(){

var str = "Welcome to the logic-practice.com ";

document.getElementById('para').innerHTML = str.italics();

}

</script>

</body>



</html>

Output

Upon executing the code and pressing the specified HTML button, the resulting output will be -

In the following example, we will utilize the alert method to present the output. Let’s examine the outcome of this next example, as it may yield a different result.

Example2

To observe the output, we need to click on the specified HTML button.

Example

<html>

<head>



</head>

<body>

<h1> Hello World :) :) </h1>

<p> Click the following button to see the effect. </p>

<p id = "para"> </p>

<button onclick = "fun()"> Click me </button>

<script>

function fun(){

var str = "Welcome to the logic-practice.com ";

alert(str.italics());

}

</script>

</body>



</html>

Output

Upon executing the code and selecting the specified HTML button, the resulting output will be -

We can observe the structure of the output by utilizing the alert function. Even though we are employing the identical method on the same string in both instances, the format of the resulting output varies.

Input Required

This code uses input(). Please provide values below: