Custom data types in js | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Custom data types in js

Is it possible to create your own data type in js and store them in a variable? For example, const component = "<h1>Hello World</h1>" The variable above is possible as it's storing a string but what if I wanna do something like, const component = <h1>Hello World</h1> Is it possible? How can I do that?

30th Apr 2024, 8:29 PM
Xinzeo
Xinzeo - avatar
3 Réponses
+ 3
The short answer is No. javascript only provide the class method to create new data types. const component = new ClassName() you cannot create another primitive types
30th Apr 2024, 10:10 PM
White Shadow
White Shadow - avatar
+ 1
Yes, it's possible to create your own data types in JavaScript using object-oriented programming principles and you have to achieve this is by creating custom objects or classes. About the example you mentioned, to achieve this outside of a React environment, you would typically need to transpile the JSX syntax into regular JavaScript. However, you can create a simple function that mimics JSX behavior by returning DOM elements created with JavaScript. JSX is a syntax extension for JavaScript often used with React to describe what the UI should look like.
1st May 2024, 3:03 AM
`нттየ⁴⁰⁶
`нттየ⁴⁰⁶ - avatar
0
Got it, thanks ^^
1st May 2024, 12:55 AM
Xinzeo
Xinzeo - avatar