Understanding Data types and Methods of primitives
π₯ Primitive Data Types
π₯ Composite Data Types
In JavaScript, there are several data types that variables can hold. Here's a brief explanation of each:
- Number: Represents numeric data, including integers and floating-point numbers.
- String: Represents textual data, enclosed in single
('')
or double("")
quotes. - Boolean: Represents a logical value, either
true
orfalse
. - Undefined: Represents a variable that has been declared but has not been assigned a value yet.
- Null: Represents the intentional absence of any object value.
- Symbol: Represents a unique and immutable value that may be used as the key of an object property.
- Object: Represents a collection of key-value pairs, where values can be of any data type (including other objects).
JavaScript allows treating primitives (like strings, numbers, etc) as objects, providing methods to call on them.
A primitive is a value of a primitive type, which includes string
, number
, bigint
, boolean
, symbol
, null
, and undefined
. On the other hand, an object is capable of storing multiple values as properties and can be created using curly braces {}
. Additionally, functions in JavaScript are also considered objects.
Objects in JavaScript allow us to store functions as properties
For example:
let john = {
name: "John",
sayHi: function() {
alert("Hi buddy!");
}
};
john.sayHi(); // Hi buddy!
Built-in objects like Date, Error, and HTML elements already exist, each with their own properties and methods.
In JavaScript, although primitives are intended to be lightweight and fast, there's a need to access methods and properties on them like objects. To address this, the language employs a solution where a special "object wrapper" is created temporarily to provide the additional functionality required, and then it is discarded.
Each primitive type in JavaScript, including String
, Number
, Boolean
, Symbol
, and BigInt
, has its own corresponding "object wrapper" that provides different sets of methods. For example, the string method toUpperCase()
is used to capitalize a string, as mentioned below:
let str = "Hello";
alert( str.toUpperCase() ); // HELLO
Referring to the above example;
When calling str.toUpperCase()
, here's what happens:
- Since
str
is a primitive, a special object is temporarily created with access to methods liketoUpperCase()
. - The
toUpperCase()
method executes and generates a new string, which is then displayed by thealert
. - After the method call, the special object is discarded, leaving the original primitive
str
unaffected.
Numbers also have their own methods. For example, toFixed(n)
rounds the number to the specified precision:
For example:
let n = 1.23456;
alert( n.toFixed(2) ); // 1.23