Check out a free preview of the full Getting Started with JavaScript, v2 course

The "new" Lesson is part of the full, Getting Started with JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle differentiates between situations where new should be used to create instances of object representations and when not to use new, and instead by omitting it allow for type conversion to occur.


Transcript from the "new" Lesson

>> Kyle Simpson: Now there are built in fundamental objects that we wanna look at. And many of these are ones that actually come historically, they were kinda copied from a language like Java. And that's why they all have capital letters like capital O Object and capital A Array and F Function.

Those kind of look like they do the class names from a language like a Java or maybe a C++. And in JavaScript, we have those they create object wrappers around these values, representations of the values. And we want to use the new keyword to instantiate instances of those.

But we have some other fundamental objects that we don't want to use the new keyword. Specifically string number and boolean, those we want to use those as functions. We don't wanna put the new keyword in front of them. Because if we call string or number or boolean with some value, it actually changes it into that type.

And in just a little bit, we're gonna be talking about changing the types of our values if we have a string and we want to make it into a number or vice versa. So those three turned out to be really useful in what we're gonna call type conversion, conversion of a type.

Here's an example of using the new in front of one of those constructor forms of a fundamental object. Saying new capital D Date, that's how we can make a date object and then use something like the toUTCString method to represent this long form of that date and time of that object.

But then you will notice on line 5 I'm using capital S String as a function to convert something that is a number like my college GPA, and yes that's actually is the GPA that I graduated with from college. Then that, you can take your numeric form of the GPA and turn into a string, that's why in the comments on line six, it's actually got quotes around it because now it's a string.

So using new in front of the fundamental objects where we wanna make object representations and skipping the new keyword whenever we wanna call it to do a type conversion, like we're doing on my font.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now