Create a function that prints the current date and time using function declaration, function expression, and arrow function.
Function declaration
function getDate () {
console.log(new Date());
};
getDate();
Arrow function
const getDate = () => { console.log(new Date)};
getDate();
Function expression
const getDate = function () {
console.log(new Date());
};
getDate();
> Fri Apr 12 2024 10:44:38 GMT+0100 (West Africa Standard Time)
Top comments (5)
Also, would be worth telling which type of function is it, because readers who donβt know any other way of defining functions, will think this is the standard, and it might be nowadays (if we talk modern ES - EcmaScript), but you can also define them with the
function
keyword.Hello Bruno, thanks for your feedback and for dropping your insights.
Very nice. You could also define the function in one line by doing this:
A function body composed of a single-line block doesn't need curly braces! π
That's correct, thanks for your valuable feedback.
Exactly π you donβt need a return statement on an arrow function which has only one line of code inside the block, or in this case when you can define all the logic like that.