The symbol.valueOf() is an inbuilt function in JavaScript which is used to return the primitive value of a given symbol object.
Syntax:
Symbol().valueOf();
Here Symbol() is the symbol object whose primitive value is to be found.
Parameters: This function does not take any parameter.
Return value: This function returns the primitive value of the given symbol object.
JavaScript code to show the working of this function.
Example-1:
<script> // Some symbol objects are created const symbol1 = Symbol('Geeks'); const symbol2 = Symbol("Geeks"); const symbol3 = Symbol(123); const symbol4 = Symbol(); // Calling the symbol.valueOf() function var result1 = symbol1.valueOf(); var result2 = symbol2.valueOf(); var result3 = symbol3.valueOf(); var result4 = symbol4.valueOf(); // Getting the primitive value console.log(result1); console.log(result2); console.log(result3); console.log(result4); </script> |
Output:
> Symbol(Geeks) > Symbol(Geeks) > Symbol(123) > Symbol()
Example-2:
<script> // Some symbol objects are created const symbol1 = Symbol('Geeks' + 'for' + 'Geeks'); const symbol2 = Symbol(2+3); const symbol3 = Symbol(10/5); const symbol4 = Symbol(1, 2, 3); // Calling the symbol.valueOf() function var result1 = symbol1.valueOf(); var result2 = symbol2.valueOf(); var result3 = symbol3.valueOf(); var result4 = symbol4.valueOf(); // Getting the primitive value console.log(result1); console.log(result2); console.log(result3); console.log(result4); </script> |
Output:
> Symbol(GeeksforGeeks) > Symbol(5) > Symbol(2) > Symbol(1)
In the above code, it can be seen that the parameter of symbol object should be a single parameter otherwise it considers the first element as the parameter and remaining are discarded. If the parameter is an arithmetic operation then it considers them as the result of the operation as the parameter.
Reference: https://devdocs.io/javascript/global_objects/symbol/valueof
Recommended Posts:
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | What is JavaScript ?
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to call a function that return another function in JavaScript ?
- How to Check a Function is a Generator Function or not using JavaScript ?
- JavaScript | Math.ceil( ) function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


