Quantcast
Channel: Envato Tuts+ Code
Viewing all articles
Browse latest Browse all 5160

Node.js Succinctly: Basic Node.js Programming

$
0
0

This chapter will describe some basic Node.js programming. Node.js uses the JavaScript language to write code. If you have experienced programming in JavaScript, you will find it easier to write Node.js code. 

Defining Variables

variable_type is a JavaScript data type. It may be a numeric or string type. variable_name is your variable name. Here's an example of declaring a Node.js variable: 

We can assign values to our variables: 

We also can assign values directly when declaring variables: 

You can see that we must define data types such as String and Number before defining our variable. In Node.js or JavaScript, we can declare a variable without defining a specific data type. We can use var for the data type. If we assign a string value then our variable will become a string data type. 

In general, we can declare a variable name with any name, but you must avoid usage of JavaScript keywords. Here is the list of keywords to avoid: 

  • break
  • case
  • catch
  • continue
  • debugger
  • default
  • delete
  • do
  • else
  • finally
  • for
  • new
  • package
  • private
  • protected
  • public
  • function
  • if
  • implements
  • in
  • instanceof
  • interface
  • return
  • static
  • switch
  • this
  • throw
  • try
  • typeof
  • var
  • void
  • while
  • with

Comment

You may explain how to work on your code as a note or comment. In Node.js, we can write a note or a comment using // and /* */ syntax. The following is a sample code: 

Arithmetic Operations

Node.js supports four basic arithmetic operations: addition, subtraction, multiplication, and division. 

  • + is for addition.
  • - is for subtraction.
  • * is for multiplication.
  • / is for division.

The following is the code illustration for basic arithmetic using Node.js. 

Save this code into a file, for instance basicarith.js. If you run this code, you will get the program output shown below. 

The program output for arithmetic operations

Mathematical Functions

  • Math.abs(a), the absolute value of a
  • Math.acos(a), arc cosine of a
  • Math.asin(a), arc sine of a
  • Math.atan(a), arc tangent of a
  • Math.atan2(a,b), arc tangent of a/b
  • Math.ceil(a), integer closest to a and not less than a
  • Math.cos(a), cosine of a
  • Math.exp(a), exponent of a (Math.E to the power a)
  • Math.floor(a), integer closest to a, not greater than a
  • Math.log(a), log of a base e
  • Math.max(a,b), the maximum of a and b
  • Math.min(a,b), the minimum of a and b
  • Math.pow(a,b), a to the power b
  • Math.random(), pseudorandom number 0 to 1 (see examples)
  • Math.round(a), integer closest to a (see rounding examples)
  • Math.sin(a), sine of a
  • Math.sqrt(a), square root of a
  • Math.tan(a), tangent of a

To get an understanding of the math library usage, write this script: 

Save this script into a file and run it in the console using Node.js. 

You can see the output of our script below. 

he program output for basic math operations

Comparison Operators

  • == is equal to.
  • != is not equal to.
  • > is greater than.
  • < is less than.
  • >= greater than or equal to.
  • <= less than or equal to.

Let’s write a script for comparison usage for Node.js. 

You can see the program output below. 

The program output for comparison operator usage

Logical Operators

Node.js supports logical operation. These logical operators can be used to determine the logic between variables or value. You can see below how Node.js implements logical operators. 

  • && is for And.
  • || is for Or.
  • ! is for Not.

Here is the script sample that illustrates logical operation: 

Run it using Node.js and you will see the program output shown here. 

The program output for logical operation in Nodejs

Increment and Decrement

Imagine you have a value and you want this value to be incremented by 1. In general, you may implement the following script: 

We can do another solution with ++ syntax. We can rewrite our script to the following: 

We also can apply decrement in our value by using –- syntax: 

Decision

A computer can execute instructions that are clear and precise. The computer will always do exactly what you say. That’s both good and bad news. The problem is that the computer follows instructions even if they are absurd; it has no judgment with which to question anything.

Our code can decide what the computer will do. It's a decision. We can implement a decision in our program behavior like "if A is true, then do B". In Node.js, we have two options to implement a decision program: 

  • if... then
  • switch... case

if... then

The following is the syntax for a decision in Node.js:

If the condition is true, then it will execute do_something_a

Let’s write a script to implement a decision program in Node.js. `

Run it using Node.js. Here is a sample output. 

Sample output for decision program by using if then

Change a and b values, then run it again.

For alternative if-conditional usage, we can use the following form: 

switch... case

We can use switch...case syntax to implement decision behavior in our program. The following is a syntax model for switch...case

The option value can be a string or numeric data type. 

For sample illustration, write this script: 

Run it and you will get an output response, shown below. 

Program output for switchcase

You may change the option value in numeric data type. If you change it, you must change the options in case syntax. 

Iterations

One of the most powerful concepts in any programming language is that of iterations. It’s one of the things that enable a program to do far more than perform a simple calculation and quit. In this section, you’ll see how a few lines of Node.js can set up an operation to be performed potentially thousands of times.

Node.js uses for and while for iteration operation. We will evaluate these syntaxes.

For

The for statement provides this mechanism, letting you specify the initializer, condition, and increment/decrement in one compact line. The following is node.js syntax of the iteration for

Let’s start to write this script. 

We can see our initial value is 0. This will run until the condition value is not met. Each iteration will do a value addition. Here is an example of the output from running the program. 

While

Node.js has the simple form of while syntax. The following is a form you can use. 

while evaluates the condition and executes the statement if that condition is true. Then it repeats that operation until the condition evaluates as false.

Now let’s use node.js code to implement the while syntax described. 

Program output for while usage

This tutorial represents a chapter from Node.js Succinctly, a free eBook from the team at Syncfusion.


Viewing all articles
Browse latest Browse all 5160

Trending Articles