Mega Node.js tutorial

What is it?

node.js is plain JS, except it can run OUTSIDE of browsers! Let me explain. Regular JS runs through HTML, the HTML provides src links to the script, which the browser (Chrome, Firefox, MS Edge, Internet Explorer, etc...) parses and runs. However JS cannot be run outside the browser, for example to make desktop apps, without frameworks such as Electron. node.js can be run outside the browser, inside the terminal, some of which are Linux shell (LinuxOS), PowerShell (Windows), Command Line (Windows + MacOS). The node.js compiler/interpreter is actually the V8 engine, which is an/the engine browsers use to parse JS code.

If you don't believe me, according to Bing:

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser. Node.js lets developers use JavaScript to write command line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser. Consequently, Node.js represents a "JavaScript everywhere" paradigm, unifying web-application development around a single programming language, rather than different languages for server-side and client-side scripts.

Why should I use node.js?

Well, first of all, node.js is one of the most supported languages. By that, I mean there are TONS and TONS of frameworks and modules to make your life easier (much like the ecosystem Python provides with PyPi). There are frontend frameworks (like Express) and backend frameworks (like Next.js).

Also, it makes it easier to communicate between frontend JS and backend node.js since the datatype encoding is almost exactly the same.

Lastly, the syntax is great and readable and works for terminal/console apps (you can use prompt). I would recommend node.js even more than Python.

Getting Started with node.js

Developing locally:

First, you want to click here and click on your OS. Finish the setup.

Developing with preinstalled packages (using an IDE like replit.com):

Just create a new node.js Repl.

Variables

Tip: To create a comment, just type // to comment the entire line and /* and */ to do multilined (half-line) comments

There are three ways to declare variables in node.js and JS:

let varName = varValue;
var varName = varValue;
const varName = varValue;

let creates a mutable variable. Mutable means the variable can be reassigned. const creates a constant variable, which means the variable cannot be changed. var is basically the same as let.

Note: Even if an Object is put into a variable using const, its properties can be changed.

To create a String, just do "string_here". To create an Int or Float, just do int_or_float_here. If you made the int or float right, then you should see the int being highlighted in light-green. To create a bool, just type true or false (all lowercase). To create a list, the syntax is similar to Python: ["list","items","seperated","with","commas"]. To add items to the list, just type listName.push(newItem). To INITIALIZE a dictionary, type let myDict = {}. If you want to INITIALIZE the dictionary with certain keys, then just put "key":"value" seperated with commas. If you want to create new items (or reassign items), then use square brackets like so: myDict["key"] = "value or new value".

Operators

String operators

let str1 = "foo";
let str2 = "bar";

let str3 = str1 + str2; //str3 is "foobar"
let str4 = str1 * 4 + str2 * 4; // it is "foofoofoofoobarbarbarbar"

/*
Because of that, you can also use operators like += and *= with strings.
*/

Int & Float operators

let myInt = 1;
let myOtherInt = 2;

myInt + myOtherInt //that would be 3
myInt - myOtherInt //that would be -1
myInt * myOtherInt //that would be 2
myInt / myOtherInt //that would be 0.5
//% is the modulo operator
10 % 2 //0
9 % 2 //1
/*
To change variables when operating on them, use operators like this:
+= changes and adds an int
-= changes and subtracts an int
/= changes and divides an int
*= changes and multiplies an int 
*/

Boolean operators

//! makes anything its opposite
!true //false
!false //true
/* 
Funny note: you can stack up !s like this:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!true would be valid
run it to see what it would be!
*/

//|| is the or gate
true || false //true
false || false //false

//&& is the and gate
true && true // true
true && false // false

//== is the equality operator
5 == 1 + 4 //true
5 == 1 + 3.999999 //false

//!= is the not-equal operator
5 != 1 + 3.88888 //true
5 != 1 + 4 //false

//>= is the greater than or equal operator
5 >= 4 //true
5 >= 5 //true
5 >= 6 // false

//<= is the greater than or equal operator
5 <= 4 //false
5 <= 5 //false
5 <= 6 // true

//> and < are comparison operators
5 > 4 //true
5 > 5 //false
5 > 6 // false

Converters

String to Int:

parseInt("43") //43

ANYTHING to String:

43.toString() //"43"

Boolean to Int:

+true //1
+false //0

//or

Number(true) //1
Number(false) //0

//or using ternary operators
(true ? 1 : 0) // 1
(false ? 1 : 0) //0

Functions

Functions are pretty important. Functions can take parameters to modify how the function behaves. Functions can also reference values using the return keyword.

function funcName(param) {
  return param + 'oof'
}
//so
funcName("oof means ") //returns "oof means oof"
//if i created a function without return, then it would reference void
function v() {
  //code
}
v() //void

OO in node.js

IMPORTANT! This tutorial is not about Object Oriented theory so go check out some object oriented tutorials here.

Objects

To create an Object, do this:

const myObj = {
  propName: propValue,
  etc...
}
//Owing to the fact that functions can be passed as parameters and variables, we can have functions in objects

Or, if you want a blueprint, you can make a constructor.

function Constructor(param1, param2, param3) {
  return {object}
}
let myObject =  Constructor(a, b, c)

Classes

I much prefer classes to Objects.

class ClassName {
  constructor (params) {
    this.whatever = whatever; //this refers to the current instance
  } 
  //no need for function keyword
  fun1 {}
  fun2 {}
}

To set/get attributes of an object, use .

Console

console.log(something) //prints out to console
console.error(error) //print out text in red
console.clear() //clears console
prompt(prompt) //gets user input, prompting them with the param prompt

Ifs

if (bool) /*(is true)*/ {
  //code
} else {
  // this executes if the bool is not true
}
//if the bool is true, then code will execute
//if it is not true (false), then the else will execute
//you can also only do an if without an else
if (true) {
  console.log("Lol");
}

Loops

While Loops

While loops are loops that execute as long as a bool is true.

while (cond) {
  //code
}

For loops

To create a for loop, just type something like this:

for (var iterator = 0; iterator <= loopRange; iterator+=1) {
  console.log(iterator);
}
//that will repeat loopRange + 1 times

Basically, that code inits a variable iterator. iterator automatically increments itself everytime the code inside the loop executes. The loop executes as long as the condition in the middle is true. For loops are similar to while loops, just they have an iterator.

Ternary operators

A ternary operator is hard to explain, but an example is below:

(true ? "is true" : "is false") //that will be "is true"
(false ? "is true" : "duck") //that will be "duck"

This is sort of like if (bool) va1 else va2 in Kotlin.