JavaScript Tutorials

Overview

JavaScript has 8 Datatypes: String, Number, Bigint, Boolean, Undefined, Null, Symbol, and Object.

Simple Examples:

// String:
let firstName = "Emily";
let lastName = "VonDuck";

Note

JavaScript strings are contained within either double quotes or single quotes. There is no difference.

“Emily VonDuck”
‘Emily VonDuck’

// Number:
let length = 8;
let weight = 1.5;

Note

All JavaScript numbers are stored as decimal numbers (floating point).

8 is stored as 8
8.00 is stored as 8
8.18 is stored as 8.18

// Bigint:
let x = BigInt("738905435368978785421");

Note

BigInt is a datatype that is used to store integer values that are too big to be represented by a normal JavaScript number.

// Boolean (true or false):
let x = 3;
let y = 4;
let z = 5;

(x < y)       // Returns true
(x > z)       // Returns false

Note

Booleans can only have two values: true or false. They are often used in conditional statements.

// Undefined (the variable has no value yet):
let car;      // Value is undefined, type is undefined

// (or the variable has its value emptied):
car = undefined;

Note

Don’t confuse “undefined” with an empty variable. An empty variable has a value which is an empty string.

car = “”;

// Null (the variable has no value yet):
let car = null;      // Value is defined, type is null

// Null (using dot notation):
obj.propertyName = null;

// Null (using bracket notation):
obj["propertyName"] = null;

Note

Null indicates that the variable is defined but currently does not hold any value.

Combining Data Types:

JavaScript works from left to right. And when trying to combine a number with a string will treat the number as a string.

In the following, it will first add the two numbers, and when it reaches the string will convert the number to a string in order to combine it with the string.

let x = 12 + 4 + "Maserati"; // 16Maserati

In the following, since the string comes first and the number 16 will then be combined with it, it will be converted to a string “Maserati16”. Then when combined with another number, that number will also need to be converted to a string first.

let x = "Maserati" + 12 + 4; // Maserati124

The typeof Operator

The typeof operator returns the type of a variable or expression.

typeof 7              // Returns "number"
typeof 3.14           // Returns "number"
typeof (5)            // Returns "number"
typeof (2 + 4)        // Returns "number"
typeof "4"            // Returns "string"

JavaScript Notes:

  • When using JavaScript, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
  • JavaScript is a case-sensitive language; firstName is NOT the same as firstname
  • JavaScript variables are case sensitive (x is not the same as X)
  • Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on
  • JavaScript variables must begin with a letter, $, or _

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.