There are five fundamental, or primitive, forms of data in Javascript. Strings, integers, booleans, undefined, and null are the five most fundamental forms of data. These are known as basic data types. A single variable can only hold one kind of data.
1. String
2. Number
3. Bigint
4. Boolean
5. Undefined
6. Null
7. Symbol
8. Object
The object data type can contain:
1. An object
2. An array
3. A date
A string (or a text string) is a series of characters like "Gideon Arias".
Strings are written with quotes. You can use single or double quotes:
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<p>Strings are written with quotes. You can use single or double quotes:</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
let placeName1 = "Germany";
let placeName2 = 'Germany';
document.getElementById("demo").innerHTML =
placeName1 + "<br>" +
placeName2;
You can use quotations within a string as long as they don't match the quotes around it:
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<p>Howdy! It's me again partner ;)</p>
<p>Let me show you how to use quotations within a string as long as they don't match the quotes around it:</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
let answer1 = "It's alright";
let answer2 = "He is called 'Gideon'";
let answer3 = 'He is called "Gideon"';
document.getElementById("demo").innerHTML =
answer1 + "<br>" +
answer2 + "<br>" +
answer3;
All JavaScript integers are stored as decimal (floating point) numbers.
Numerical expressions can be expressed with or without decimals:
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<h2>JavaScript Numbers</h2>
<p>Numbers can be written with, or without decimals:</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
let x1 = 68.00;
let x2 = 69;
let x3 = 1.71;
document.getElementById("demo").innerHTML =
x1 + "<br>" + x2 + "<br>" + x3;
Extra large or extra small numbers can be written with scientific (exponential) notation:
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Numbers</h2>
<p>Scientific (exponential) notation can be used to write extremely big or extremely tiny numbers:</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
let y = 47e8;
let z = 47e-8;
document.getElementById("demo").innerHTML =
y + "<br>" + z;
BigInt in JavaScript
All integers in JavaScript are stored in 64-bit floating-point representation.
JavaScript BigInt is a new (ES2020) datatype that may be used to hold integer values that are too large to be represented by a standard JavaScript Number.
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h1>JavScript Bigint</h1>
<p>A BigInt can not have decimals.</p>
<p id="demo"></p>
<p>You cannot perform math between a BigInt type and a Number type.</p>
</body>
</html>
JAVASCRIPT
let x = BigInt("632849185291045712909571204189215179800139141298");
document.getElementById("demo").innerHTML = x;
Booleans have only two possible values: true or false.
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Booleans</h2>
<p>Booleans can have two values: true or false:</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
let x = 300;
let y = 300;
let z = 400;
document.getElementById("demo").innerHTML =
(x == y) + "<br>" + (x == z);
JavaScript arrays are written with square brackets.
Array items are separated by commas.
The following code declares (creates) an array called cars, containing three items (car names):
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2>
<p>Array indexes are zero-based, which means the first item is [0].</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
const cars = ["Toyota","Suzuki","Polestar"];
document.getElementById("demo").innerHTML = cars[0];
Curly braces are used to denote JavaScript objects.
The names and values of object properties are separated by commas.
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
const person = {
firstName : "Gideon",
lastName : "Arias",
age : 18,
eyeColor : "Black"
};
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
A variable with no value in JavaScript has the value undefined. The kind is also undefined.
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Operators</h1>
<p>Variables can be emptied if you set the value to <b>undefined</b>.</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
let car = "Toyota";
car = undefined;
document.getElementById("demo").innerHTML = car + "<br>" + typeof car;
Undefined has nothing to do with an empty value.
An empty string has a lawful value as well as a type.
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p>An empty string has both a legal value and a type:</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
let car = "";
document.getElementById("demo").innerHTML =
"The value is: " +
car + "<br>" +
"The type is: " + typeof car;