Strings in TypeScript – Part 3

Strings in TypeScript is divided in three parts:

This section has the details of following methods:

toLocaleLowerCase toLocaleUpperCase toLowerCase
toUpperCase toString valueOf
toLocaleLowerCase()

This method will convert all the characters of a string in lower case representing the current locale.
Syntax

string.toLocaleLowerCase();

Below is a sample code

var str1 = new String("I like to eat MANGOES, WHICH are YELLOW in color");
console.log("Original String: " + str1);
console.log("Lower-case String: " + str1.toLocaleLowerCase());

Below is the output of above code

toLocaleUpperCase()

This method will convert all the characters of a string in upper case representing the current locale.
Syntax

string.toLocaleUpperCase();

Below is a sample code

var str1 = new String("I like to eat MANGOES, WHICH are YELLOW in color");
console.log("Original String: " + str1);
console.log("Upper-case String: " + str1.toLocaleUpperCase());

Below is the output of above code

toLowerCase()

This method will convert all the characters of a string in lower case.
Syntax

string.toLowerCase();

Below is a sample code

var str1 = new String("I like to eat MANGOES, WHICH are YELLOW in color");
console.log("Original String: " + str1);
console.log("Lower-case String: " + str1.toLowerCase());

Below is the output of above code

toUpperCase()

This method will convert all the characters of a string in upper case.
Syntax

string.toUpperCase();

Below is a sample code

var str1 = new String("I like to eat MANGOES, WHICH are YELLOW in color");
console.log("Original String: " + str1);
console.log("Upper-case String: " + str1.toUpperCase());

Below is the output of above code

toString()

This method returns the string representing the specified object. You can perform all the string operations on this converted object.
Syntax

object.toString();

Below is a sample code

var str1 = "I like to eat MANGOES, WHICH are YELLOW in color";
console.log("Original String: " + str1);
console.log(str1.toString());

Below is the output of above code

valueOf()

This method returns the primitive value of the String Object.
Syntax

string.valueOf();

Below is a sample code

var str1 = "I like to eat MANGOES, WHICH are YELLOW in color";
console.log("Original String: " + str1);
console.log(str1.valueOf());

Below is the output of above code

Number Object Methods in TypeScript

The Number object have only the default methods that are present in any other object. Properties of Number object are explained in another article.
Below are some of the commonly used methods:
valueOf()
toString()
toPrecision()
toLocaleString()
toFixed()
toExponential()

toExponential()

This methods returns the exponential notation of the Number object in the string data type.
Syntax:

numberVariable.toExponential(numberOfDecimals?:number)

The parameter in this method is a number which specifies the number of decimal digits after decimal point. It is optional.
Below is an example:

var num1=123.456789;
var result=num1.toExponential();
console.log(result);
var result1=num1.toExponential(2);
console.log(result1);

Below is the output of above code:

toFixed()

This function is used to fix the the decimal digit in a fractional number. We pass the number of digits we want after decimal point as the parameter.
Syntax:

numberVariable.toFixed(numberOfDecimals?:number)

Below is an example:

var num1=123.4567;
console.log(num1.toFixed());
console.log(num1.toFixed(1));
console.log(num1.toFixed(5));

Below is the output of above code:

toLocaleString()

This method converts a number object into a human readable string representing the number using the locale of the environment.
Syntax:

numberVariable.toLocaleString();

Below is an example:

var num1=123.456;
var str=num1.toLocaleString();
console.log(num1.toLocaleString());
console.log("typeof str: " + typeof(str));

Below is the output of above code

toPrecision()

This method returns a string representing the number of digits to the mentioned precision. Parameter takes the number of digits for the precision and is optional.
Syntax

numberVariable.toPrecision(precisionNumber?:number)

Below is an example:

var num=12.3456;
console.log(num.toPrecision());
console.log(num.toPrecision(2));
console.log(num.toPrecision(4));
var str=num.toPrecision(2);
console.log(typeof(str));

Below is the output of above code

toString()

This method will convert a object into a string type. We can then use String methods on this object.
Syntax:

variable.toString()

Below is an example

var num=1234;
console.log(num.toString());
console.log(typeof(num.toString()));

Below is the output of above code:

valueOf()

Returns the primitive value of an object. This can be any object, number, string etc.
Syntax:

variable.valueOf();

Below is an example:

var num = new Number(100); 
console.log(num.valueOf());
var str = new String("Hello World!!"); 
console.log(str.valueOf());

Below is the output of above code:

Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started