Skip to main content

Posts

Showing posts with the label Javascript

Javascript Ternary Operator

This tutorial will help you learn how to replace an  if/else  statement with a more concise shorthand syntax called the ternary operator. The conditional operator – also known as the ternary operator – is an alternative form of the  if/else  statement that helps you to write conditional code blocks in a more concise way. The syntax for the conditional operator looks like this: conditional ? expression_when_true : expression_when_false ; conditional operator basic syntax First, you need to write a  conditional expression  that evaluates into either  true  or  false . If the expression returns true, JavaScript will execute the code you write on the left side of the colon operator ( : ) when it returns false, the code on the right side of the colon operator is executed. To understand how it works, let's compare it with a regular  if/else  statement. Let's say you have a small program that assigns different exam grades depending on your exam score: When you have a score higher th

module.exports vs exports

One of the most powerful things about software development is the ability to reuse and build upon the foundations of other people. This code sharing has helped software progress at an amazing rate. Such a wonderful mechanism is critical on a micro-level for both individual projects and teams. For Node.js, this process of code sharing – both within individual projects and in external npm dependencies – is facilitated using  module.exports  or  exports . How Node Modules Work How do we use module exports to plug an external module, or sensibly break our project down into multiple files (modules)? The Node.js module system was created because its designers didn't want it to suffer from the same problem of broken global scope, like its browser counterpart. They implemented  CommonJS specification  to achieve this. The two important pieces of the puzzle are  module.exports  and the  require  function. How module.exports works module.exports  is actually a property of the  module  object