//new
Javascript has some interesting tools such as new
or this
built in that you probably have seen used but never new how to use it outside of a tutorial or project you followed from someone else. I know I haven't really covered new
just yet either and hope you find this to be a valuable resource!
What is the new keyword? Let's first examine it.
// new
Example
const myDate = new Date('August 11, 2025');
console.dir(myDate);
console.log(myDate.getFullYear());
Testing it out
If you open the console, you will see tons of methods on the console.log(myDate). Where did the myDate.getFullYear
come from?
When you create a any object, number, or string, it's extended off the constructor - If you look at all the types we have - you will see they are technically an object. In your console you can type Array
and the result will be a function Array()
. These functions, when we add new
to the front of that, it returns an object.
const myNumber = new Number (100);
If we type in myNumber
to the console - you will see a bunch of methods that exist on it. This is what we mean when we say everything in Javascript is an object.
Let's go back to the date example above.
Testing it out a step further
We have an instance of Date
and if we type typeof myDate
in the console, you will see it's an object
. Similarly, if we type myDate instanceof Date
we will see the result is true
.
Literals
We don't need to type in new
when creating an array.
For Example: const names = ['drew', 'audrey'];
we don't need to put new
in front of it. Why is that? If we use the console for type of names
we see it's an object. We see that it's an Object (it's not an Array, which is a special kind of object). But if we say names instanceof Array
we see it's true
. Why is that? That's because this is what we consider literal syntax. const names = ['drew', 'audrey'];
=== const names = new Array('drew', audrey');
. Since the left side of this example is "Array Literal, we don't have to use the "new" keyword.
Let's make a pizza!
function Pizza() { console.log('Making a pizza'); } const pepperoniPizza = Pizza(); console.log(pepperoniPizza);
We can run this and you can see we get undefined. That makes sense because the function didn't return anything. What happens if we just add new
to that?
function Pizza() {
console.log('Making a pizza');
}
const pepperoniPizza = new
Pizza();
console.log(pepperoniPizza);
You get a new empty Pizza{}
(pizza object)! It creates a new instance object of that function instead of what ever has been returned from that function.
My Final Thoughts and Ask
By using the new
keyword in Javascript, that creates a new object that is an instance of what ever function you have made it from.
If you found this article helpful, share/retweet it and follow me on twitter @codingwithdrewk!
There is a lot more to learn in Javascript, the new
keyword is a tricky one but I hope that this article helps to clear it up!
Drew is a seasoned DevOps Engineer with a rich background that spans multiple industries and technologies. With foundational training as a Nuclear Engineer in the US Navy, Drew brings a meticulous approach to operational efficiency and reliability. His expertise lies in cloud migration strategies, CI/CD automation, and Kubernetes orchestration. Known for a keen focus on facts and correctness, Drew is proficient in a range of programming languages including Bash and JavaScript. His diverse experiences, from serving in the military to working in the corporate world, have equipped him with a comprehensive worldview and a knack for creative problem-solving. Drew advocates for streamlined, fact-based approaches in both code and business, making him a reliable authority in the tech industry.