Site icon Eval

JavaScript data types: JavaScript Tutorial & Demo 7

JavaScript Data Types JavaScript Tutorial & Demo 7

This article lists all of the JavaScript data types and what they can do. When it’s possible, comparisons are drawn with other languages.

Data Types: What does it mean?

Data types are used to group one type of data in programming languages, which means that data types tell you what kind of data can be stored and used in a program.

In simple terms, a value in JavaScript always has a certain type. This type is called a data type, and that type is called a data type.

There are many different types of data that we can use in a JavaScript app. 

Example

let a = 102; // Number
n = 3.1421; // Number
let Name = "John Hills" // String

Here, we’ll talk about how data types work in JavaScript and the important data types of the language.

Variations of data types

Two main types of data are called “data types.” Each of these types is broken down into sub-types.

Primitive data types

Primitive data types are predefined data type supported by a programming language. All primitive data types are immutable, i.e., they can’t be changed or altered. It is also known as in-built data type.

Non-primitive data types

Data types are made by the user and not predefined by the language itself are non-primitive data types. They are mutable, i.e., they can be changed or altered. Non-primitive data type are also known as derived data, reference data types, structural data types,

Primitive Data Sub Types

Number

When you think about a number type, you think about both integers and floating points. They are usually used to doing math things. The Number type shows numbers that start at -(253 – 1) and goes up to (253 – 1), the minimum value.

Example

let x = 20;
let y = 11.5;

BigInt

BigInts let us safely deal with large integers, both positive and negative, that don’t fit into the range of the Number type. This isn’t used very often when the data fall outside the range of the Number data type.

Example

const big = BigInt(9006599259740999);
const big = BigInt(-9006599259740999);

String

People use strings to store data with characters, like names or ages. Each letter in the string has its number. They are used to store many different kinds of data, like numbers, characters, and more.

Example

let text = "Jennifer Shaw";
let college = “Wheaton Academy”;

Boolean

People who use the Boolean type only get true or false as a return value. Use this type to see if something is right or wrong. They play a big part in conditional statements because they are binary.

In a way, an undefined value is very similar to a null value because it makes its own type of value as well. The word “undefined” means that a value has not been given.

Example

<!DOCTYPE html>
<html>
<body>
	<h1>Demo: JavaScript Boolean</h1>
	<p id="p1">3 < 4 = </p>
	<p id="p2">a > b = </p>
	<p id="p3">a < b = </p>
	<p id="p4">a + 2 < b + 1 = </p>
	<script>
		var a = 30, b = 20;
		var result = 3 < 4; // true
		document.getElementById("p1").textContent += result;
		result = a > b; // true
		document.getElementById("p2").textContent += result;
		result = a < b; // false
		document.getElementById("p3").textContent += result;
		result = a + 2 < b + 1; // true
		document.getElementById("p4").textContent += result;
	</script>
</body>
</html>

Symbol

There is only one symbol that can’t be changed or added to. It can be used as the key to an Object property. In more simple terms, symbols make unique IDs for things. We will go over this in more detail in future javascript tutorials.

Example

const x = Symbol('hello');
console.log(x.description); // hello

Null value

A null value says that a variable is empty or that its value is unknown. In simple terms, it has no value, so it has only one value – zero.

Example

let name;
console.log(name); // undefined

Non-primitive Data Sub Types

Object

Objects are used to store groups of data and more complicated things. They can be thought of as a group of traits. In the world of objects, there are two types of properties: the data property and the accessor property. Both of them have certain properties. In future lessons, we will learn more about these properties.

Example

<html>
<body>
<script>  
emp={id:111,name:"Santiago Damien",salary:16500}  
document.write(emp.id+" "+emp.name+" "+emp.salary);  
</script>
</body>
</html>

Arrays

When we use arrays, we store data in a sequence of memory locations. This makes it easier to get to them, and it saves us time when we have to look for the right data. It doesn’t work the same way for objects. Arrays use square brackets instead of curly ones. Each value in an array has a “key” that isn’t defined in the code itself but rather a number that tells you where it is in the array.

Example

// empty array
const aE = [ ];
// array of numbers
const aN = [ 3,6,9,4,5];
// array of strings
const aS = [ 'sleep', 'eat', 'repeat'];
// array with mixed data types
const aM = ['work', 'run', 1, true];

Regular Expression (RegExp)

In JavaScript, regular expressions are termed as objects. JavaScript has a built-in type called RegExp that lets you work with regular expressions more effectively. String and RegExp both have methods that use regular expressions to do powerful pattern-matching and search-and-replace on text. The JavaScript RegExp class represents regular expressions. Regular expressions can look for and change strings that match a pattern.

Example

const Rege = new RegExp('abc123');

Functions

A JavaScript function is also a type of object. They have a unique quality: They can be called on or put into action at any time. The name of a function is typed after parentheses. Functions can do many things, like print, do something with the console, do the math, etc.

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>Function Example:</p>
<p id="demo"></p>
<script>
function Fx1(a, b) {
  return a * b;
}
document.getElementById("demo").innerHTML = Fx1(5, 5);
</script>
</body>
</html>

Example

Article Category: JavaScript Tutorials

More JavaScript Tutorials

Exit mobile version