In this post, we will quickly go through String() and Number() functions in JavaScript.

 

1. String()

 

Both the following ways of defining a string in JavaScript are same.

 

var str = "Hello World";
console.log(str); //Hello World

 

var Str = new String("Hello World");
console.log(Str.toString()); //Hello World

 

toUpperCase() and toLowerCase() methods of String() object.

 

var Str = new String("Hello World");
console.log(typeof Str); //Object
console.log(Str.toUpperCase()); //HELLO WORLD
console.log(Str.toLowerCase()); //hello world

 

charAt() return the character at the specified position. inedxOf() returns the index of a particular character. You can specify a second argument to indexOf() that would start reading the string from that character.

 

console.log(Str.charAt(4)); //"o"
console.log(Str.charAt(12)); // returns nothing as char does not exists at index 12
console.log(Str.indexOf('o')); //4
console.log(Str.indexOf('o', 5)) //7

 

slice(arg1, arg2) method will return a substring of the string. The substring starts from arg1 index of the string and ends at arg2-1 index of the string.

 

split(arg) will convert the string to an array. The argument specified in split will act as separator.

 

var Str = new String("Hello World-this-is a great world");

console.log(Str.slice(1,4)); //ell
console.log(Str.split(' ')); //[ "Hello", "World-this-is", "a", "great", "world" ]
console.log(Str.split('-')); //[ "Hello World", "this", "is a great world" ]

 


 

2. Number()

 

Two similar ways of getting the same result. One is direct, another one is by using constructor function.

 

var num = 12;
console.log(num); //12
var Num = new Number('12');
console.log(Num.valueOf()); //12

 

Some standard properties of Number() functions are as follows:

 

console.log(Number.MAX_VALUE)
console.log(Number.MIN_VALUE)
console.log(Number.POSITIVE_INFINITY)
console.log(Number.NEGATIVE_INFINITY)

 

0 Do You Love This Article ?

CT

Hello All! You are reading the author(s) profile of CorvineLabs Team. We may be 1,2 or ..any in number. Let that remain withing us. You enjoy our write up. If you want to join the team than do Contact Us.

Leave a Reply

Your email address will not be published. Required fields are marked *