Wednesday, September 18, 2024

Primitive Types in JavaScript


 

Key Concepts

Like any other programming language, JavaScript supports multiple data types. Understanding these types and the different operations you can perform on them is essential to coding effectively with JavaScript.


JavaScript Data Types

The data types supported by JavaScript fall into two categories, primitive and object types. In this post, I provide an overview of primitive types and introduce object data types in the next post.


Dynamic Typing

JavaScript is a dynamically typed language. Dynamic typing means that the type of a variable is known at runtime, but not at compile time. Therefore, you do not need to declare the type of your variables. The language interpreter will extract the type of a variable from the value assigned to it at the runtime. This feature makes writing code more flexible, but less error-tolerant. Some errors may only be detected at runtime and not early at compile time.


Primitive Data Types

Primitive data types are the basic blocks for data representation and handling. They are built-in in most programming languages and used to construct more complex data structures. Primitive types are immutable. In other words, their values cannot be changed. There are seven primitive data types in JavaScript, which I present in the second table below with a brief description and examples.


Type Checking

JavaScript provides the ‘typeof’ operator to check the type of a variable.


Type Conversion

JavaScript supports implicit and explicit type conversion, which I explain in the following table.


Type Conversion in JavaScript

Conversion

Definition

Code snippet

Code Output

Implicit

Converting types when necessary

let str = '12' + 8;

console.log(str);

console.log(typeof(str));

128

string

Explicit

Converting types using functions

let num = Number('475');

console.log(num);

console.log(typeof(num));

475

number


Primitive Types

Here is a summary of the primitive types with examples in JavaScript.


Primitive Types in JavaScript

Type

Description

Example

Code Snippet

Code Output

Checking

Checking Output

Number

integer and float numbers

18, 45.5

let exp = 18 * 3 + 45.5;

console.log('exp:', exp);

exp: 99.5

console.log(typeof(18));

number

BigInt

large integers

9007199254740991n

let expBI = 9007199254740991n / 30n;

console.log('expBI:', expBI);

expBI: 300239975158033n

console.log(typeof(9007199254740991n));

bigint

String

sequence of characters

"nice", 'life'

let str1 = "Hello";

let german_str1 = str1.replaceAll("e", "a");

let str2 = "World!";

let german_str2 = str2.replaceAll("o", "e")

.replaceAll("r", "").replaceAll("d", "t")

console.log(german_str1 + " " + german_str2);

Hallo Welt!

console.log(typeof("nice"));

string

Boolean

logical values ‘true’ and ‘false’

true

let found = false;

let c = "b";

let str = "beautiful dream";

if (str.indexOf(c)>-1){

found = true;

}

if (found){

console.log(c, "is found in", str);

}

b is found in beautiful dream

console.log(typeof(true));

boolean

Undefined

a declared variable to which no value is assigned

let item;

let item;

if (item == undefined){

console.log(item);

}

undefined

console.log(typeof(undefined));

undefined

Null

no value is provided

let item = null;

let item = null;

if (item){

console.log(item);

} else {

console.log("Your item is null");

}

Your item is null

console.log(typeof(null));

object

Symbol

unique and immutable identifier

let item = Symbol("name");

const sym1 = Symbol();

const sym2 = Symbol("name");

const sym3 = Symbol("name");

console.log(sym1 === sym2);

console.log(sym2 === sym3);

false

false

console.log(typeof(Symbol("name")));

symbol

No comments:

Post a Comment

Blog Posts

Enhancing Performance of Java-Web Applications

Applications built with a Java back-end, a relational database (such as Oracle or MySQL), and a JavaScript-based front-end form a common and...