Skip to content

Variable existence checking

Environment variable checking

// Check if a environment variable exists
if (!process.env.JWT_KEY) {
  throw new Error('JWT_KEY must be defined');
}

// '!' tells typescript that this variable DOES exist
process.env.JWT_KEY!;

Object checking

router.get('/api/users/currentuser', (req, res) => {
  // SAME THING
  if (!req.session || !req.session.jwt) {
    return res.send({ currentuser: null });
  }

  // SAME EXPRESSION
  if (!req.session?.jwt) {
    return res.send({ currentuser: null });
  }
});