1.5 Predefined Objects in JavaScript
There are lots of built-in objects in JavaScript. You've used some of them, such as arrays:
let arr = [1, 2, 4, 8, 16]; console.log(arr);
and functions (yes, they are objects):
function sum(a, b) { return a + b; }
Common Objects
Here are some common objects worth introducing:
- Wrapper Objects (
Number
,String
.Boolean
)
- Date
- Math
Wrapper Objects
Wrapper object types (
Number
, String
, Boolean
) serve as object "versions" of their corresponding primitive type. We can declare and initialize them as we would declare any other object type:
let firstName = new String("Mark"), lastName = new String("Smith"); // same as let firstName = "Mark", lastName = "Smith";
Wrapper objects can be used in a similar fashion to their corresponding primitive type (i.e. concatenation on String objects):
let firstName = new String("Mark"), lastName = new String("Smith"); let name = new String(""); name = firstName + " " + lastName; console.log(name, ": Hi!");
However, like many other objects, wrapper objects are far more useful and complex under the hood.
Take a look at the following code:
let name = "Mark Smith"; console.log(name.substring(0, 4));
The
substring
method isn't a part of the name variable. What's going on right now is this:
let name = "Mark Smith"; // behind the scenes of the name.substring(0, 4); let tmp = new String(name); console.log(temp.substring(0, 4)); tmp = null;
Thus, we can make a conclusion: Wrapper objects have properties and associated methods.
Now, doesn't this just seem like extra work to accomplish the same goals? Sometimes yes, sometimes no. There are benefits to using wrapper objects. Here's an example:
let str = "hello"; str.extra = "Lucas"; console.log(str.extra); // undefined
We get an
undefined
, which is weird since we clearly defined it. The thing with primitive data types is that they only exists for one line of code.
So, after
str.extra = "Lucas";
, JavaScript has forgotten about the .extra
property. To not get an undefined, we can do this:
let str = new String("hello"); str.extra = "Lucas"; console.log(str.extra); // Lucas
Although wrapper objects and their corresponding primitives can behave the same way a lot of the time, objects and primitives are fundamentally different.
There are some cases where this difference is visible to us.
let name1 = “Mark”, name2 = new String(“Mark”); console.log(typeof name1); // will output string console.log(typeof name2); // will output object
Another example would be when using the strict equality operator:
let x = 3, y = new Number(3); console.log(x == y); // true console.log(x === y); // false
Date Object
Date objects, as their name suggests, keep track of dates. Each date object stores a single moment in time. Here is an example of how the date object is used:
let myDate = new Date(); //stores date (accurate to the millisecond) of the instant this instance of Date is created console.log(myDate.toString()); // display string of date in readable format (date displayed is accurate to the second) console.log(Date.now()); // this static method returns date of the instant when it is called
A complete list of methods in the Date class can be found on this webpage.
Math Object
While we can create instances of the Math class, they won’t be too helpful since most of Math’s methods and properties are static.
We used the Math class a few times before, once when we used the random function (
Math.random()
).Math properties are commonly used mathematical constants, such as
Math.PI
and Math.E
.Math methods are common math functions, such as
abs()
, exp()
, pow()
, etc.A list of all methods of the Math class can be found here.
Copyright © 2021 Code 4 Tomorrow. All rights reserved.
The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.