Today I am going to write a little bit about Number, String and Array in detail.
A number is a simple wrapper object for representing and manipulating numbers like 5 or 0.5. You can express numbers by using letters too. For example, 0b101, 0o13, 0x0A are numbers.
To create a new number value you have to call the Number(value) constructor. Also, other types of values can be converted to numbers by using the Number() function.
There are 2types of number syntax which are — Literal syntax and Function syntax. The literal syntax is just the number whereas the function syntax is passing the values through the Number function.
Now about the properties of the number. The number has a lot of properties. Some of them are -
MAX_VALUE — it gives the largest possible number
MIN_VALUE — it gives the smallest possible number
POSITIVE_INFINITY — returns positive infinity
NEGATIVE_INFINITY — returns negative infinity
NaN — returns Not A Number value
There are also various methods in Number. By calling these method our works get easier in js. Some methods are -
.isNan() — it returns the value is number or not
.isFinite() — it returns the value is finite number or not
.isInteger() — it returns the value is integer number or not
Math, a built-in object, offers advanced mathematical functions and constants. It is related to numbers. So it just works with number types. Let’s see how it works in js.
Math.Pi — returns the pi value which is 3.1416
Math.abs(x) — it returns the absolute value of x. For example
Math.abs(-5) // 5
Math.floor(x) — returns the nearest integer > x.
Math.floor(5.3) // 5
Math.ceil(x) — returns the nearest integer < x.
Math.floor(5.6) // 6
Math.sqrt(x) — returns the square root of x
Math.sqrt(9) // 3
Math.sin(x) — returns the sine value of x
Math.sin(0) // 0
There are also Math.cos(x) and Math.tan(x) to get the cosine and tan value of x.
You can create string by using primitive method or directly by calling the String function’s constructor. Though it is string type but it is most likely to an array. So you can get the length of a string by calling the length() method. Now we will see some of methods we can use in our js codes. For example,
const string = “Hello World”;string.length(); // 11string.indexOf(o); // 5string.toUpperCase(); //HELLO WORLDstring.toLowerCase(); //hello worldstring.charAt(5); // o
const jsArray = [‘index1’, ‘index2’, ‘index3’, ‘index4’];
jsArray; // index3
To add item to end
// jsArray = [‘index1’, ‘index2’, ‘index3’, ‘index4’, ‘index5’]
To remove from end
jsArray.pop(); // jsArray = [‘index1’, ‘index2’, ‘index3’, ‘index4’]
To add item to start
// jsArray = [‘index0’, ‘index1’, ‘index2’, ‘index3’, ‘index4’]
To remove item from start
// jsArray = [‘index1’, ‘index2’, ‘index3’, ‘index4’]
To find an index of an item
jsArray.indexOf(‘index2’) // 1
// jsArray = [‘index4’, ‘index3’, ‘index2’, ‘index1’]