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 :
As desired, Primitive stays primitive, i.e., a single value.
It allows us complete access to properties and methods of numbers, symbols, booleans, and strings.
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
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
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
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.
Is JavaScript easy to learn? Yes. If you truly wish to learn something, it will be extremely easy for you to grasp.
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.
Is JavaScript dead? No, javascript is far from being dead. It is only being applied in more and more things with each passing day.
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.
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?