1.5 Data TypesWhat exactly are data types?NumbersBooleansStringsArraysObjectstypeof OperatorEmpty valuesUndefinedNullDifference between undefined and nullComplex Data
1.5 Data Types
JavaScript has different data types: numbers, booleans, strings, objects, and arrays, and more.
Here are some examples:
let length = 16; // type: number let lastName = "Johnson"; // type: string let obj = { firstName: "John", lastName: "Doe" }; // type: object
What exactly are data types?
In programming, data types are an important concept. It tells the computer what a piece of code is and how it should be treated.
Let's go over the different types:
Numbers
Every number you can think of fall under the
number
type. Numbers can be written with or without decimals.
let numOne = 34.00; // written with decimals let numTwo = 34; // written without decimals
Extra large or extra small numbers can be written with scientific notation.
let x = 123e5; // 12300000 let y = 123e-5; // 0.00123
Booleans
Booleans can only have two values: true or false.
let isRaining = false; let isSunny = true;
Strings
A string (or a text string) is a series of characters like this sentence.
Strings are written with quotes. You can use single or double quotes.
let carNameOne = "Volvo XC60"; // using double quotes let carNameTwo = 'Wolwo YT90'; // using single quotes
You can use quotes inside a string, as long as they don't match the quotes surrounding the string.
let answer1 = "It's alright"; // Single quote inside double quotes let answer2 = "He is called 'Johnny'"; // Single quotes inside double quotes let answer3 = 'He is called "Johnny"'; // Double quotes inside single quotes
Arrays
Arrays are written with square brackets. The items are separated by commas.
The following code declares (creates) an array called
sports
, containing three items (sport names):let sports = ["Basketball", "Soccer", "Badminton"];
Objects
Objects are written with curly braces.
The object properties are written as
name:value
. The properties are separated by commas.// note: this is one line let person = { firstName: "John", lastName: "Doe", age: 50, eyeColor: "Blue" };
The object (person) in the example above has 4 properties:
firstName
, lastName
, age
, and eyeColor
. typeof
Operator
You can use the JavaScript
typeof
operator to find the type of a JavaScript variable. The
typeof
operator returns the type of a variable or expression:typeof ""; // Returns "string" typeof "John"; // Returns "string" typeof "John Doe"; // Returns "string" typeof 0 // Returns "number" typeof 314 // Returns "number" typeof 3.14 // Returns "number" typeof (3) // Returns "number" typeof (3 + 4) // Returns "number"
Empty values
An empty value has nothing to with
undefined
. An empty string has both a legal value and type.
let car = ""; // the value is "", the type is string
Undefined
A variable without a value has the value
undefined
. The type is also known as undefined
. let car; // value is undefined, type is undefined
Any variable can be emptied by setting the value to
undefined
. The type will also be undefined
. let car = "Volvo XP90"; car = undefined; // value is undefined, type is undefined
Null
null
is "nothing". It is supposed to be something that doesn't exist. However, the data type of null
is considered a object.You can consider it a buy in JavaScript that
typeof null
is an object. It should be null
. You can empty an object by setting it to
null
. let person = { firstName: "John", lastName: "Doe", age: 50, eyeColor: "blue" }; person = null; // now value is null, but type is still an object
You can also empty an object by setting it to
undefined
. let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}; person = undefined; // now both value and type is undefined
Difference between undefined
and null
undefined
and null
are equal in value, but different in type. typeof undefined; // undefined typeof null; // object
Complex Data
The
typeof
operator can return one of two of complex types: function
and object
. The
typeof
operator returns object
for objects, arrays, and null. The
typeof
operator does not return object
for functions. typeof { name:'John', age: 34 }; // returns "object" typeof [1,2,3,4]; // returns "object" (not "array", see note below) typeof null; // returns "object" typeof function myFunc(){}; // returns "function"
The
typeof
operator returns object
for arrays are objects.Previous Section
1.4 Printing to ConsoleNext Section
1.6 VariablesCopyright © 2021 Code 4 Tomorrow. All rights reserved.
The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.