Types in Node

Course Home | What are Types and Objects? | Code Example(s)

Variables are declared by using the special keyword var before setting a name for the variable and then using the equals sign and the value you want to store to the variable name.

var theNameOfMyVariable = theValue;

You can also declare a variable but not assign it a value until later on in your script, like so:

var theNameOfMyVariable;


theNameOfMyVariable = theValue;

Node is known as a loosely or dynamically typed language. What this means is that anything can be assigned to a variable using var like in the examples below:

// See https://thefriendlytester.co.uk/programming/lessons/typesandobjects for a more detailed explanation of types
var myInt         = 33;
var myFloat       = 33.33;
var myBoolean     = true;
var myString      = "Mark loves cake";
var myIntArray    = [1, 2, 3];
var myStringArray = ["Mark", "Node", "Testing"];
var myFunction    = function(){
                       return "Hello";

// Because myInt is an integer, Node allows me to do math with it
// If you run this, you should get 330 in the console
console.log(myInt * 10);

// Same applies to floats. This should return you 333.3
console.log(myFloat * 10);

// This will print true

// This will print "Mark loves cake"

// This will print "["Mark", "Node", "Testing"]" as it's an array of strings

// But we can also print a single string from the array by providing an index value
// If we wanted the string "Node" we can call myStringArray[1]. Notice how the second
// item is called with the number one. That's because arrays are zero indexed meaning
// 0 willl return the first array item.

// This will print the output of our function which will be "Hello"

This means you can quickly create code without worrying about declaring type for each variable as well as do some interesting things with functions, which we learn about later.

Have a look at the code example for some code you can download and run and see how these types behave.

Next Lesson ➔

Mark Winteringham's photo

Mark Winteringham


Mark Winteringham is a tester, toolsmith and the Ministry of Testing DojoBoss with over 10 years experience providing testing expertise on award-winning projects across a wide range of technology sectors including BBC, Barclays, UK Government and Thomson Reuters. He is an advocate for modern risk-based testing practices and trains teams in Automation in Testing, Behaviour Driven Development and Exploratory testing techniques. He is also the co-founder of Software Testing Clinic a community raising awareness of careers in testing and improving testing education. You can find him on Twitter @2bittester.