Programming Tutorials

Handling String in JavaScript

By: aathishankaran in JavaScript Tutorials on 2007-03-29  

In JavaScript, the substring() method is used to extract a portion of a string. It takes two parameters: the starting index and the ending index of the substring. Here's an example:

const str = "Hello, world!";
const subStr = str.substring(0, 5); // "Hello"

In the example above, the substring() method is used to extract the first five characters of the string.

Formatted strings, also known as template literals, allow for easy string interpolation and formatting. They use the backtick (`) character to define the string and allow for placeholders, which are denoted by a dollar sign followed by curly braces (${expression}). Here's an example:

const name = "Alice";
const age = 25;
const formattedString = `My name is ${name} and I am ${age} years old.`;
console.log(formattedString); // "My name is Alice and I am 25 years old."

In the example above, the formattedString variable is defined using a template literal, with placeholders for the name and age variables. When the string is logged to the console, the placeholders are replaced with their corresponding values.

Keep in mind the context in which you can use the formatting methods. You cannot use them outside a JavaScript script as a substitute for HTML formatting tags. 

String Method Example Returns HTML Code

anchor("anchorName")

"Section3".anchor

<A NAME="Section3">Section('Section3")3</A>

big()

"Just save the name".big()

<BIG>Just save the name</BIG> 

Blink()

"Why ask why".blink()

<BLlNK>Why ask why</BLlNK> 

Boid()

"Liveandlearn".bold()

<B>Liveandlearn</B>

Fixed()

"JavaScriptcode".fixed()

<TT>JavaScript code</TT>

fontcolor("color")

"JavaPrograms".fontcolor("green")

<FONTCOLOR"green">JavaPrgrams</FONT>

fontsize(size)

"Impact".fontsize(3)

<FONT SIZE="3">lmpact</FONT>

Italics()

"Java Script Unleashed".italics()

<I>JavaScript Unleashed</I>

Link("URL")

"See My Home Page". link("http://www.myhomepage.com/")

<A HREF="http//www.myhomepage.com/> SeeMyHomePage</A>

Small()

"TinyTim".small()

<SMALL>Tiny Tim</SMALL>

Strike()

"GameCanceled"strike()

<STRIKE>GameCanceled</STRIKE>

Sub()

"Submarines".sub()

<SUB>Submarines</SUB>

sup()

"Superman".sup()

<SUP>Superman</SUP>

toLowerCase()

"SeeSPOTrUN".toLowerCase()

seespotrun

ToUpperCase()

"let's go to the BEACH".toUpperCase()

LET'S GO TO THE BEACH

Working with Special Characters 

When working with strings in any language, you will discover certain characters that are difficult to use. JavaScript enables you to work with these special case characters by using a backslash character (\) followed by the character or its code. Table below lists the JavaScript in line symbols. 

Symbol

Description 

\t

Tab

\n

New line

\r

Carriage return

\f

Form feed

\\

Backslash

\b

Backspace

\"

Double quote

\'

Single quote






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in JavaScript )

Latest Articles (in JavaScript)