JavaScript Basics
Let’s learn the basics of JS data types.
We'll cover the following...
Data types
JavaScript has three primitive datatypes–string
, number
, and boolean
. There are five reference datatypes–Object
, Array
, Function
, Date
, and RegExp
. Arrays, functions, dates, and regular expressions are special types of objects, but conceptually, dates and regular expressions are primitive data values and happen to be implemented in the form of wrapper objects.
Below, we have an example of JS data types:
let string = "Johnson"; // Stringlet number = 16; // Numberlet bool = true; // Booleanlet object = {firstName:"John", lastName:"Doe"}; // Objectlet array = ['Jim', 'Shawna']; // Arrayfunction func() { return (5 * 19); } // Functionlet date = new Date() // Dateconsole.log("String is: " + string)console.log("Number is: " + number)console.log("Object is: " + object.firstName + " " + object.lastName)console.log("Array is: " + array)console.log("Function is: " + func())console.log("Date is: " + date.toUTCString())
String data
A string is a sequence of Unicode characters. String literals, like "Hello world!"
, 'A3F0'
, or the empty string ""
are enclosed in single or double quotes. Two string expressions can be concatenated with the +
operator and checked for equality with the triple equality operator.
The number of characters in a string can be obtained by applying the length
attribute to a string. The following ...