A Glimpse of JavaScript’s String, Number and Arrays

JavaScript (js) is a dynamic client-side script language. It is also used on the server-side by Nodejs. Don’t get confused by thinking that Java and JavaScript are the same. Though they are from Oracle still they are different. There are many types in js. They are:

  • Number
  • String
  • Boolean
  • Symbol
  • Object
  1. Function
  2. Array
  3. Date
  4. RegExp
  • null
  • Undefined

Today I am going to write a little bit about Number, String and Array in detail.

Number

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.

String

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”;

Array

The JavaScript Array class is a global object that is used to create arrays, which are list-like high-level objects. Arrays are list-like structures with methods for traversal and mutation operations in their prototype. The basics of the js array are written below

To create

const jsArray = [‘index1’, ‘index2’, ‘index3’, ‘index4’];

To access

jsArray[2]; // index3

To add item to end

jsArray.push(‘index5’);
// jsArray = [‘index1’, ‘index2’, ‘index3’, ‘index4’, ‘index5’]

To remove from end

jsArray.pop(); // jsArray = [‘index1’, ‘index2’, ‘index3’, ‘index4’]

To add item to start

jsArray.unshift(‘index0’);
// jsArray = [‘index0’, ‘index1’, ‘index2’, ‘index3’, ‘index4’]

To remove item from start

jsArray.shift();
// jsArray = [‘index1’, ‘index2’, ‘index3’, ‘index4’]

To find an index of an item

jsArray.indexOf(‘index2’) // 1

To reverse

jsArray.reverse();
// jsArray = [‘index4’, ‘index3’, ‘index2’, ‘index1’]

That’s all from me. Hope you get some idea of JavaScript’s string, number and array from this write-up. I will come with more write-ups in the next few days. Till then stay tuned and follow me. Thank you.

Sources:

  1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
  2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
  3. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
  4. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
  5. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array