I’m very new to javascript, node.js and express. My question is, how do I refactor the following code to make it one line inside the function?
exports.about = function(req, res){
var mytime = new Date();
res.render('about', {title: 'about page', time: mytime.toLocaleDateString() });
};
In other words, is there a way I can compress the var mytime = new Date(); and time: mytime.toLocalDateString() into one statement?
See Frits’ answer: You can, but is there really any need? It’s nice and readable the way you’ve done it.
But if you really, really want to, this is how:
It looks a bit odd, but the
new Date()part takes precedence, so you don’t even needs parens around it (e.g., you don’t needtime: (new Date()).toLocaleDateString()). You could have them if you want, but they’re not necessary.