javascript - Express + EJS - passing arguments to EJS view -


i'm rather new node.js/express/ejs.

i've noticed when i'm passing arguments express request handler ejs view , omit argument name creates name based on variable name. so, example, in code below,

//server.js var express = require('express');  var app = express();  app.set('view engine', 'ejs');  app.get('/', function(req, res){     var products = [         { name: 'tennis ball', price: 10 },         { name: 'basketball', price: 20 }     ];          res.render('index', {products}); });   app.listen(8080);  //index.ejs <ul> <% products.foreach(function(product){ %> <%= product.name %> <% })%> </ul> 

the argument passed called "products" , view able iterate well. assume, better code readability, should have placed line instead:

res.render('index', {products : products}); 

i wonder if that's okay use both techniques?

the difference between 2 how you're defining object , properties.

{ products } tells v8 engine assign property products value of variable products in scope. called object literal property value shorthand , feature of es6.

{ products: products } long-form way create object in es6 , only way in version prior es6.

as long node version supports shorthand can use it. preference , readability, there no right or wrong way here.