Skip to content
Sign upLog in
← Back to Community
Basic NodeJS Tutorial
Profile icon

There are no Node tutorials, so I set upon the task of making one.

Part 1

Logging to the console.

To log something to the console, you use the function console.log(<text here>) Remember to enclose the text in quotes, because it is a string.
Here is an example:

console.log("Hello world")

(me making an effort not to break coding tradition :))
That is how you log things.

If statements

Here is the syntax for an if statement:

if(<conditional>){ //do this if the conditional returns true }else if(<conditional 2>){ //do this if conditional 2 returns true }else{ //if none of the above happens, do this }

You can add as many else ifs as you like, but keep in mind that you can always use the switch statement (see below)

A table of conditionals:

CondtionalWhat it does:
x > yTrue if x is greater than y
x < yTrue if y is greater than x
x >= yTrue if x is greater than or equal to y
x <= yTrue if y is greater than or equal to y
x == yTrue if x is equal to y (note: "1" == 1 returns true)
x === yTrue if x is equal to y (note: "1" === 1 returns false)
x !== yTrue if x is not equal to y
!Add to the beginning of any conditional - returns true if conditional if false


A loop syntax:

for(var i = 0; <conditional>; i++){ //code to repeat goes here }

You can make i equal to anything you want, keeping mind that if you make a loop in a loop, you need to define a different variable.
Everytime the loop repeats, i is incremented. The loop stops looping when the conditional is equal to false.
Here is an example:

for(var i = 0; i <= 100; i++){ console.log(i) }

The result:

0 1 2 3 4 5 6 7 8 9 10 11 12 ...

While loops

Here is the syntax:

while(<conditional equals true){ //do this }

Note: // is a comment


To define one:

var <varname> = <value>

The variable name can not begin with a number or special character (unless it is the underscore)

var foo = "bar"

Here we define the variable "foo", and assign it a string with the value of "bar"


var myArray = [1,2,3];

What is this?
This is an array. Arrays allow you to store many values in one variable (good for inventories in adventure games).
To access the content of an array you put the name of the variable storing the array, followed by [, the index of the item you want - remember- computers count from 0, followed by ].

var myArray = [1,2,3]; console.log(myArray[1]);



Remember, computers count from 0.

var myObj = { "thing1":"hi there", "myArr": ["you can","put arrays","in objects"], "myObj2":{ "You":"can", "Put_objects":"inOjbects" } }

What is this?
This is an object.
Objects follow key-value syntax. There are 2 ways to get the content of arrays:

Way 1:


This is "hi there"

Way 2:


This is also "hi there".
You can also combine them:


This is "can"


You can also put numbers in variables, like we did in loops:

var myNum = 0;

##Getting User input:
To get user input, we use something called a library, a hunk of code created by somebody else.
The library to get user input is called 'readline-sync', and (as far as I know) has one method: question()
Here is how you use it:
step 1: Define a variable containing the library:
var read = require('readline-sync');

  • note: the require function adds libraries to your code *
    step 2: call question() and store the user input in a variable:
    var input = read.question("How are you? ");
    That is all you need to do.
    The output:
How are you? Good

You can spit it back at them:
console.log("You are doing "+input+"! I am doing good");
The output now:

How are you? Good You are doing Good! I am doing good

Try making a program to see how old they are and to see if the user can drink. The answer is in the repl.


Functions are pieces of code that you can run with a single line of code.
You define them by doing the following:

function <function name>(<parameters>){ //code goes here }

The parameters a values that the function uses. Here is an example:

function sayHi(greetText){ console.log(greetText); console.log("How are you?"); }

To call it * note: calling the function runs the code inside it *



Sup' How are you
  • note: When you call the function, the things inside are the parameters. When you put something in there, it stores what you put in as (in this example) greetText. greetText works as a variable, and you can use it as one *

Returning values:

To return a value you use the return statement followed by the value you wish to return. Here is a full code example:

function add(num1,num2){ //to accept multiple parameters, you use a comma return num1+num2 } var addedNums = add(1,3); console.log(addedNums);



This is all you need to create amazing programs.
** remember to upvote, and have fun coding! **

Profile icon
Profile icon
Profile icon
Profile icon