primitive data types and wrapper objects(type conversion) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

primitive data types and wrapper objects(type conversion)

Is what I'm saying correct? string, number, object, function, symbol,etc, are primitive data types.(objects are String Number, Date, Object,etc)when we use them as object they convert to object automatically example: (2.9).toFixed()//this number convert to Number object. There is an exception here that function are actually objects. try this : function f(a) { return a; } var b=new f(function () {}) console.log (typeof b) // function result is function. while if we pass an non-function type to this object-constructor, result is object.What other reason could it have been other than that the functions are callable objects?

14th Apr 2021, 7:16 PM
Mehran
Mehran - avatar
7 Answers
+ 6
You got some of the primitives correct but function and object incorrect. Here's the full list of 7 with their corresponding wrapper objects in brackets: string (String) number (Number) boolean (Boolean) symbol (Symbol) bigint (BigInt) undefined null Primitives don't have methods. You're right to say they get converted to the corresponding wrapper objects to allow you to call methods. One thing to note is that once you're finished with the wrapper object, it's removed. So, you're left with a primitive again. So, we've touched on primitive types. The other major category is object types (also called reference types). There are 3: Object, Array, and Function. So yes, functions are objects. More specifically, they are callable objects as you correctly say. They are also first-class objects, which means they can have methods and properties and methods just like any other object. Examples showing the length property and toString() method of Function objects: const f = (x) => {} console.log(f.length) // 1 const g = () => {} console.log(g.toString()) // "() => {}"
14th Apr 2021, 9:02 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 2
CamelBeatsSnake ok, I ignored some primitive types like bigint
14th Apr 2021, 10:14 PM
Mehran
Mehran - avatar
+ 1
there is very interesting answer
23rd Jun 2021, 12:41 PM
What Is The Meaning Of The Life
What Is The Meaning Of The Life - avatar
0
Yes, what you are saying is correct. In JavaScript, string, number, object, function, symbol, etc. are considered primitive data types. However, when we use them as objects, they are automatically converted to their respective object types. For example, when we use the toFixed() method on a number like (2.9), it converts the number to a Number object. Functions in JavaScript are also objects, and they have the ability to be called. When we create a new instance of a function using the object constructor, like var b = new f(function (https://www.survey.onl/talktokfc-new-zealand/) {}), the typeof b will still be "function". However, if we pass a non-function type to the object constructor, the result will be an object. The main reason functions are considered callable objects is because they can be invoked or called.
10th Aug 2023, 10:12 AM
William
0
Remarkable contribution, and your steadfast dedication to this topic is truly commendable. It's uplifting to witness increased interest in this subject on the platform, and your commitment to its exploration is greatly appreciated. here you can also visit this link https://carxstreetapk.download/ check this.
21st Jan 2024, 12:06 PM
Arham
0
Discover the contrasts between Juice Plus and Balance of Nature supplements, exploring their ingredients, benefits, and potential impacts on health to make an informed decision for your nutritional needs. https://juicerhunter.com/balance-of-nature-vs-juice-plus/
13th Feb 2024, 7:27 AM
Ryan James
0
You're mostly correct in your understanding of primitive data types and how they behave when used as objects. Let me clarify and expand on a few points: Primitive Data Types: String, number, boolean, null, undefined, symbol, and bigint are indeed primitive data types in JavaScript. These types are immutable and represent simple values. Objects: Objects in JavaScript are collections of key-value pairs and are used to store complex data structures. They include built-in objects like String, Number, Date, and custom objects created using constructors or object literals. Autoboxing: JavaScript has a concept called autoboxing, where primitive values are automatically converted to their corresponding object wrappers (e.g., Number, String) when accessed as objects. This allows you to call methods on primitive values as if they were objects. var num = 2.9; console.log(num.toFixed()); // Output: "3" In this example, the primitive number value 2.9 is automatically converted to a Number object when the toFixed() method is called. Functions as Objects: In JavaScript, functions are first-class citizens, which means they can be treated like any other object. Functions are indeed objects, but with additional properties and behavior that allow them to be invoked. function f(a) { return a; } var b = new f(function () {}); console.log(typeof b); // Output: "function" https://www-ikrogerfeedback.com In this example, b is an instance of the function f, so its type is indeed "function." When you pass a non-function type to the constructor of f, you'll get an object because function is a special type of object that has callable behavior. The primary reason you get typeof b as "function" even when b is an instance of f is because functions in JavaScript are objects with additional callable behavior. This behavior distinguishes them from regular objects created with a constructor.
12th Apr 2024, 9:48 AM
andrea924breaux