Categories
JavaScript Basics

Highlights of JavaScript — Date, Time, and Functions

Spread the love

To learn JavaScript, we must learn the basics.

In this article, we’ll look at the most basic parts of the JavaScript language.

Change Date and Time

The Date instance lets us change various parts of the date and time.

setFullYear lets us set the year of a Date instance.

setMonth lets us set the month of a Date instance. It accepts a number from 0 to 11, with 0 being January and 11 being December.

setDate lets us set the day of the month.

setHours lets us set the minutes past an hour.

setSeconds lets us set the seconds past the minute.

And setMilliseconds lets us set the milliseconds past the second.

For example, we can call setFullYear by writing:

let d = new Date();
d.setFullYear(2040);

or:

let d = new Date();
d.setMonth(11);

The rest can be called the same way.

Functions

Functions are blocks of code that we can use repeatedly to do what we want.

We called functions before with setFullYear , setMonth , etc.

They may take 0 or more arguments.

For example, we can create our own tellTime function by writing:

function tellTime() {
  let now = new Date();
  let jr = now.getHours();
  let min = now.getMinutes();
  alert(`${hr}:${min}`);
}

We called getHours to get the hours and getMinutes to get the minutes.

Pass Data to Functions

Functions can have parameters so we can pass data to them.

For example, we can write:

function greetUser(greeting) {
  alert(greeting);
}

The greetUser method accepts the greeting parameter, and we use the value as the argument for the alert function.

This way, we can specify the message for the alert.

We can call our function by writing:

greetUser('hello james');

Now we should see an alert with the ‘hello james’ text displayed.

We can separate multiple parameters and arguments with commas. For example, we can write:

function showMessage(greeting, name) {
  alert(`${greeting}, ${name}`);
}

The showMessage function has the greeting and name parameters.

Then we can call showMessage by writing:

showMessage('hello', 'mary');

With parameters, we can customize how functions can be called.

Return Data in Functions

If we want to get the data computed by functions, then we add the return statement to return the data we want from the function.

For example, we can write:

function getTotal(subtTotal, taxRate) {
  return subTotal * (1 + taxRate);
}

We created the getTotal function that takes the subTotal and taxRate parameters.

Then we return the computed result by using the return statement.

We can call the getTotal function by writing:

const total = getTotal(100, 0.05);

We pass in the numbers for the subTotal and taxRate and assign the returned value to total .

Therefore, total is 105.

Conclusion

We can change the date and time with Date instance methods.

Also, we can create functions with parameters and return data in it so we can use that result somewhere else.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *