Before we start into the exercises for this section, I want to do a pre exercise just to cover some of the topics that we've dealt with in the fundamental section. So let me jump to sublime. This is what we're going to do. In this little exercise, we're first going to create a date object. And then we're going to print out using console dot log the date in the format of day slash month, slash year. So European format for for printing out the date with slashes.
So let's go ahead and see how that's done. First off, we need to create a variable date and we need to use the date constructor to create a new date object. So that will take care of that for Now we have to extract from this data object, the day of the month, the month itself, and the year so that we can then format it in a day slash month slash year format. So how do we go about doing that? Well, a lot of times, you'll run into JavaScript situations and you can't remember the exact command. You know, there's something out there to do it, but you're not sure what it is.
So let's, and that's a situation with the date object, you don't use the date object all the time. And so that can be something that you have to frequently look up whenever you need to use it. So the best way to do that is simply google. So I'm going to type JavaScript date object in my search, and you'll get a bunch of different references that you can take a look at. I usually like to go through go to W three schools. So as you can see there Give some information about how to use the data object talks about some properties of the data object.
And then it talks about the methods of the date object. So we're going to need to use get date, because that returns the day of the month, get full year because that returns the year. And then also get month returns the month and remember a zero base from zero to 11. So those are the three methods we're going to need to use. So jumping back now, to sublime. Now, there's a couple ways to do this.
Once we have our date object. I'm going to do it by creating a separate variable for the day, month and year and then put those variables together. There are other approaches as approach I'm going to take, I'm going to deal with all the variables at the top. So I'm going to put a comma, tab in and let's get the day first and the day is going to be equal to dot get date. That'll give us the the day. Now let's do month and month will be equal to date, dot get month.
So we're using our dot notation. And then finally the year, and that will be equal to date dot get full year. And then we close that with semicolon. All right, so we've got our variables. Now we need to print that out. However, remember, the month is zero based, so we should do something to the month first.
Before we print it out, we need to add one to it. Remember the increment operator. That's what we're going to use month plus plus semi colon that will increment the month by one. Now this is the nice thing about it. JavaScript being dynamically typed and coercion which we talked about in one of our movies. We don't know for sure what, what format, the data is in coming from the get month method of the date object.
We don't know what format that is, we don't know if it's a string or a number. But because the increment operator is only used with numbers, it will coerce it to a number and then increment it. Okay. And then when we display it, it will convert it back to a string. So now that we have the month incremented, let's go and display console dot log day and concatenate that to slash and then concatenate that to month catenate that to another slash, and then finally concatenate that to year. Then we close it with a semicolon.
All right, that should give us what we need. So we're going to set this This, I'm going to go to my HTML file, I just want to copy the path. It'll make it easier for testing. I'm gonna go ahead and paste in the path and press return. Okay, so here's our, our page. Let me open the console.
And there, there it is. Day is the 21st of July 7 2016. So instead of displaying a six, it displayed a seven because we incremented the month. All right, before we leave this exercise, let's look at how. Let's look at how we would use a template string to to output this. So console dot log.
Then in parentheses, then remember we start with the backtick. And then we want to enter the day first and so we put the dollar sign with the curly braces indicate that the day should go there, then a slash. Then dollar sign, curly braces month, slash dollar sign curly braces year, close out with a semicolon. Let's go ahead and save that. And let's check that one out as well. So if we refresh this page, oh, notice we got an error, syntax error unterminated template literal.
And so the template string literal, I did not terminate properly with a backtick. You probably noticed that as I was typing it in, so let's go back here and put in our back tick right there. Save it again. Go to the page refresh. And there we have it printed twice. Alright, so that reviewed a few concepts that we covered in this section.
Now, let's have you Do a couple of exercises that will also help review