Table of contents
1.
Introduction 
2.
Primitives
3.
Objects
4.
Primitive as an object
5.
Frequently Asked Questions
6.
key takeaways
Last Updated: Mar 27, 2024

Methods of primitive

Author Kabir Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

In javascript, we have a way of working with the primitives(strings, numbers, and much more), which resemble the working of objects.

But we still have to keep in mind that both objects and primitives are different. Let’s have a quick look at the differences between them.

Primitives

Objects

Value type is Primitive Stores multiple values in the form of properties
There are seven types of Primitives Can be easily created using {}

 

Let’s have a deeper look into them.

Also See, Javascript hasOwnProperty

Primitives

They are of 7 types, and the types are as follows:

  1. Number
  2. Bigint
  3. Symbol
  4. String 
  5. Boolean
  6. Null
  7. Undefined
     

Now let's also have a look at objects!

Objects

The objects can easily be created with { }. Let's go through some code and see how this looks.

{
  name: "Aventador",
  model: "Lamborghini",
  price: 4000000
}
You can also try this code with Online Javascript Compiler
Run Code


Functions are also termed as objects in Javascript, and these are easy to use.

We can easily store a function as a property of the objects here. Let’s have a look at the code of this as well.

let kabir = {
  name: "Kabir",
  sayHi: function() {
    alert("Hey People!");
  }
};

kabir.sayHi(); // Hey People!
You can also try this code with Online Javascript Compiler
Run Code


Output:

 

Hey People!

 

So here, the object being developed is 'kabir', and the method that is coming in is 'sayHi'.

Multiple pre-existing objects work with various things like errors, elements, HTML, dates, etc., and all these have different methods and properties, respectively.

But we have a problem to deal with!

These objects are pretty much heavier than the primitives. So let’s see how we can have a primitive as an object.

Primitive as an object

The below text describes the entire paradox of JavaScript:

  • If we desire to do various things with a primitive like a string or a number. It would be perfect for accessing these primitive using methods.
  • Primitives should tend to be as fast and lightweight as possible.
     

We get an awkward solution for this. Let's have a look :

  1. As desired, Primitive stays primitive, i.e., a single value.
  2. It allows us complete access to properties and methods of numbers, symbols, booleans, and strings.
  3. We create a special "object wrapper" that provides us with extra functionality to make that work.
     

The "object wrappers" are completely different from the primitive types and are as follows:

  • String
  • Number
  • Boolean
  • Symbol
  • BigInt
     

And hence these provide an entirely different set of methods.

For example, we have a string method called str.toUpperCase(), which returns a capitalized str.

Let’s understand it with the help of a code:

let str = "Coding";

alert( str.toUpperCase() ); // CODING
You can also try this code with Online Javascript Compiler
Run Code


Output

 

CODING

 

Wasn’t that pretty simple?

Let’s see how this method works out:

  • We have our string str, which is primitive. Thus, when we access its property, the creation of a special object takes place that is aware of the string's value, which further has useful methods like toUpperCase().
  • The method returns a new string after we use the alert operation.
  • Finally, the primitive str is left alone, and the object is destroyed.
     

Even after providing us with methods, the primitives remain lightweight.

The Javascript engine helps u to optimize this process. It also tends to skip the extra object creation. 

A number also has methods of its own. For example, toFixed(n) rounds off the given number to the given precision.

Let’s have a look at it using a code:

let num = 1.23456;
console.log(num.toFixed(3));
You can also try this code with Online Javascript Compiler
Run Code


Output

 

1.235

 

The number gets rounded off to 3 places here.

The point to be noted is that we can use the constructor's String/Boolean/Number only internally.

Languages like java give opportunities to create "wrapper objects" for primitive, and they have the following syntax:

new Number(1);
or
new Boolean(false);
You can also try this code with Online Javascript Compiler
Run Code


Though it is completely possible to do the same in javascript, it isn't recommended as this might give you some unwanted outcomes.

For Example:

console.log(typeof 0);
console.log(typeof new Number(0)); 
You can also try this code with Online Javascript Compiler
Run Code


Output

 

number

Object

 

When using 'if,' the objects are always truthy. And here we will have the alert to show the following output:

let zero = new Number(0);
if (zero) {
  console.log("In this case, zero is truthy!");
}
You can also try this code with Online Javascript Compiler
Run Code


Output

In this case, zero is truthy!

We can now also make use of the same functions Number/Boolean/String without using new.

With the help of these, a value is directly converted into a string, a boolean, and a number.

Lets see an example:

let num = Number("12"); // convert a string to number
console.log(typeof num);
You can also try this code with Online Javascript Compiler
Run Code


Output

 

number

 

This sums up that Primitive has a variety of useful and extremely helpful methods (excluding null and undefined). These methods operate smoothly via temporary objects. The engines of javascript have proper tuning to enhance this entire process internally. Hence, we conclude that it is not expensive at all. You can practice by yourself with the help of an online javascript compiler.

Let’s have a look at some Frequently asked questions.

Frequently Asked Questions

  1. What do you use JavaScript for?
    JavaScript is used to create the front-end and backend functionalities of a web application, mobile application, and even a desktop application.
     
  2. Is JavaScript easy to learn?
    Yes. If you truly wish to learn something, it will be extremely easy for you to grasp.
     
  3. How do I start JavaScript?
    To start with JavaScript, you can look for online resources such as Coding Ninjas, w3schools, solo learn and understand it on your own, or you can take a mentor-led course by Coding Ninjas and understand each concept in depth.
     
  4. Is JavaScript dead?
    No, javascript is far from being dead. It is only being applied in more and more things with each passing day.
     
  5. Is Python better than JavaScript?
    Python is a different language than JavaScript having different features and functionalities. If you wish to compare both the languages for a particular use case, you need to do extensive research on your own to say if one language is better than the other.
     
  6. Is JavaScript front end or backend?
    JavaScript is both a front end and a backend language.

key takeaways

In this blog, we have a look at the primitives and their methods. We were also able to figure out the main key differences between primitives and objects. Also, we looked at some frequently asked questions as well that made our concepts clearer.

Want to learn more about development? And want to build projects in javascript?

You can check out the blog 20 Projects With JavaScript Code Examples for project ideas. Don’t stop here. Check out our Full-stack development course to learn web development from scratch.

 

Live masterclass