Chapter 5: Data Type
Object Wrappers and
Prototypes
Almost everything in JavaScript is an object. Every data type in JavaScript is wrapped by
an object, providing some functionality when trying to get accessed. All JavaScript objects
inherit from a prototype that belongs to its type. A prototype is simply a term to define all
properties and methods available for a specific data type. A prototype method can be used by dot
notation, as the syntax defined below:
yourData.prototypeMethod()
And a prototype property as below:
yourData.prototypeProperty
In this list, you will see the signature of the methods as well. Any argument between brackets
(“[“ and “]”) means they are optional. On the other hand, a method from a data type is called
after the data type’s name:
dataType.method()
String
There are several prototype methods and properties available for strings. Here are the
most common ones. A full list of prototypes and methods can be found here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
String prototypes
String.prototype.length
This property returns the length of the string in UTF-16 code units. This property is read-
only.
46
console.log("Hello World".length) // 11
Tip: A prototype method or property can be used through a variable as well.
let myString = "Hello World"
console.log(myString.length)
String.prototype.charAt(index)
The charAt() method returns the character at the specified index.
console.log("Hello World".charAt(1)) // e
Caution: The index of a string starts from 0.
String.prototype.split([separator[, limit]])
This method will split your text by the separator you defined and returns an array.
console.log("Hello-beautiful-world".split("-
")) // [ 'Hello', 'beautiful', 'world' ]
If you want to split a string by every character, you can define an empty string as the first
argument:
console.log("Hello".split("")) // [ 'H', 'e', 'l', 'l', 'o' ]
The second argument is used for defining a limit, meaning that even if there are matching cases
for another split, it will be stopped if the limit is reached.
console.log("A-B-C-D-E-F".split("-", 2)) // [ 'A', 'B' ]
String.prototype.toLowerCase()
Will return the string converted to lowercase.
console.log("HeLLo WoRLd".toLowerCase()) // hello world
String.prototype.toUpperCase()
Will return the string converted to uppercase.
console.log("heLLo WorlD".toUpperCase()) // HELLO WORLD
47
Number
The number data type doesn’t have as many prototypes as the string data type has, but
provides many methods to work with numbers. A full list of prototypes and methods can be
found here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
Number prototypes
Number.isNaN(value)
Determines whether the passed value is NaN.
console.log(Number.isNaN(2)) // false
console.log(Number.isNaN(NaN)) // true
Number.parseInt(string)
Parses an integer from a string. In other words, this method will turn your string to a
number.
let myString = "57"
console.log(typeof myString) // string
console.log(typeof Number.parseInt(myString)) // number
Keep in mind this method will return an integer even if your string represents a float number.
let myString = "57.82"
console.log(Number.parseInt(myString)) // 57
Number.parseFloat(string)
Parses a float from a string, just like Number.parseInt() but also supports float numbers.
let myString = "23.64"
let newNumber = Number.parseFloat(myString)
console.log(newNumber) // 23.64
console.log(typeof newNumber) // number
48
Arrays
Arrays are used for storing a sequence of data. Data inside an array can be of any type
and can also be different, meaning that you can have an array of multiple elements of different
types. An array is defined using two brackets (“[“ and “]”), and a comma separates each element.
let myFavoriteColors = ['blue', 'green', 'red']
In order to access each of these specific data inside of your array, you should use the index of
that element. An array index starts from 0 and not 1 in JavaScript. This is how you would access
your data inside of an array:
let myFavoriteColors = ['blue', 'green', 'red']
console.log(myFavoriteColors[0]) // blue
console.log(myFavoriteColors[1]) // green
console.log(myFavoriteColors[2]) // red
Your index is surrounded by brackets following your variables name.
An array can hold other data types as well, like numbers, boolean, or any other valid data type.
let numberArray = [4, -9, .2, -.16, 6.4]
let booleanArray = [true, false, true, true, false]
An array can hold different data types, as well:
let mixedArray = ['A string', 4, false]
Array data can be assigned later using the index:
let petsArray = []
petsArray[0] = 'Dog'
petsArray[0] = 'Cat'
petsArray[0] = 'Iguana'
Array prototypes
Array.prototype.length
Returns the number of elements in the array.
let coordinates = [90, 50, 12]
console.log(coordinates.length) // 3
49
By setting this prop, you can create an empty array of fixed length:
let emptyArray = []
emptyArray.length = 5
console.log(emptyArray) // [ <5 empty items> ]
By assigning a length later if you have more elements than the specified length, extra elements
will be deleted from the end of the array:
let numbersArray = [87, 34, .5, -.7, 99.23]
numbersArray.length = 3
console.log(numbersArray) // [ 87, 34, 0.5 ]
Another use case of length can be when you want to access the last item in an array.
let numbersArray = [22, 97, 65, 82, 17]
console.log(numbersArray[numbersArray.length - 1]) // 17
Using this technique, you are passing the length of the array minus one as the index, which will
be the last index of that array given that array indexes start from 0.
Array.ptototype.push()
Adds zero or more items to the array and returns the new array length.
let numbersArray = [87, 34, .5, -.7, 99.23]
let count = numbersArray.push(80)
console.log(numbersArray) // [ 87, 34, 0.5, -0.7, 99.23, 80 ]
console.log(count) // 6
Array.prototype.pop()
Deletes the last element from an array and return that element.
let numbersArray = [87, 34, .5, -.7, 99.23]
let deletedElement = numbersArray.pop()
console.log(deletedElement) // 99.23
console.log(numbersArray) // [ 87, 34, 0.5, -0.7 ]
Array.prototype.shift()
Removes the first element from an array and returns that element. Just as opposite of
Array.prototype.pop() method.
let numbersArray = [87, 34, .5, -.7, 99.23]
50