Unit 2, Lesson 2

$\textbf{Datatypes in Node.JS}$
$\text{Written by:} $
$\text{Dev Singh, Class of 2022}$
$\text{Last Revised: March 2020}$

Like most scripting languages, JS is a "duck-typed" language, and therefore every variable is defined using the keywords var, let, or const. This variable can contain any type of variable. While most of them should be similar to what you have learned in CSI, let’s go over a few of them.

String

A string is simply a series of characters, like “Hello, world”. They are defined in JS as such:

In [1]:
const helloWorld = "Hello, World!"

You can use both single-quotes and double-quotes for defining strings, but please be consistent! It’s important for other developers to be able to read your code.

Number

JS only has one type of numbers (there is no int, float, etc). All numberes are, by default, double precision floating point numbers. Numbers can be written with or without decimals, and in scientific notation.

In [2]:
const daysSinceLastLogin = 3
const userRating = 6.9
const avagadrosNumber = 6.022e23

Booleans

Booleans can only hold two values: true or false. They are very simple to define in JS. They are often used in conditional evaluation (if, elif, else).

In [3]:
let isSignedIn = true
let isIMSAStudent = false
let michaelIsLame = true

Undefined and Null

JS variables without any value have the value undefined. Their type is also undefined. You can empty a variable by setting its value to undefined.

In JS, null is “nothing”. It doesn’t exist. While the value of null is null, the type is Object (this is essentially a bug in JS. null should have the type null). You can empty a variable by setting its value to null.

In [4]:
let car
console.log(car)
undefined
In [5]:
let carr = null
console.log(carr)
console.log(typeof(carr))
null
object

Arrays

Arrays in JS are the equivalent of lists in Python. They are used to store multiple values in one variable. Data types can be mixed within arrays. Members of arrays can be referred to with their index. PLEASE REMEMBER: ARRAYS START AT 0!!

In [6]:
const classes = [false, "LE2", "AmStud", "BC 1/2", ["CSI", "MSI"], "Spanish 3", false]
In [7]:
console.log(classes)
[
  false,
  'LE2',
  'AmStud',
  'BC 1/2',
  [ 'CSI', 'MSI' ],
  'Spanish 3',
  false
]
In [8]:
console.log(classes[0])
false
In [9]:
console.log(classes.length)
7
In [10]:
console.log(classes[0].length)
undefined
In [11]:
console.log(classes[4][1])
MSI
In [12]:
console.log(classes[4].length)
2

Objects

JS objects are written with curly braces {} and store information as name-value pairs. DO NOT confuse these with objects in Object-Oriented Programming; while similar in concept, they do not represent what Object-Oriented Programming is designed to do. We will discuss true Object-Oriented programming later on in the course.

In [13]:
let student = {firstName: "Dev", lastName: "Singh", hall: 1507, wing: "B", classes:[false, "LE2", "AmStud", "BC 1/2", ["CSI", "MSI"], "Spanish 3", false]}
In [14]:
console.log(student.firstName)
Dev
In [15]:
console.log(student.hall)
1507
In [16]:
console.log(student.classes.length)
7
In [17]:
console.log(student.classes[0])
false
In [18]:
console.log(student.classes[4].length)
2

Practice Exercises

  • Create an object to represent potential users on a social media network.
    • Use all the datatypes mentioned in this document
    • Use the typeof() function and some arithmetic operators (look up what these are in JS!) each at least twice

Additional Resources