JavaScript provides a variety of functions to convert data from one type to another. These conversion functions are essential for handling and manipulating data effectively. In this article, we will explore some common JavaScript functions like parseInt()
and their counterparts, explaining their usage with clear examples to help them understand.
1. parseInt()
Description
The parseInt()
function converts a string into an integer.
Syntax
parseInt(string, radix);
string: The string to be parsed.
radix: The base of the number system (2 to 36). Defaults to 10 if not provided.
Examples
parseInt("42"); // Returns 42
parseInt("101", 2); // Returns 5 (binary to decimal)
parseInt("7F", 16); // Returns 127 (hexadecimal to decimal)
2. parseFloat()
Description
The parseFloat()
function converts a string into a floating-point number.
Syntax
parseFloat(string);
- string: The string to be parsed.
Examples
parseFloat("3.14"); // Returns 3.14
parseFloat("123abc"); // Returns 123
3. Number()
Description
The Number()
function converts a value to a number.
Syntax
Number(value);
- value: The value to be converted.
Examples
Number("123"); // Returns 123
Number("123.45"); // Returns 123.45
Number("abc"); // Returns NaN
4. String()
Description
The String()
function converts a value to a string.
Syntax
String(value);
- value: The value to be converted.
Examples
String(123); // Returns "123"
String(true); // Returns "true"
5. toString()
Description
The toString()
method converts a number to a string.
Syntax
number.toString([radix]);
- radix: Optional. Specifies the base of the number system (2 to 36).
Examples
(123).toString(); // Returns "123"
(123).toString(2); // Returns "1111011" (binary)
6. Boolean()
Description
The Boolean()
function converts a value to a boolean.
Syntax
Boolean(value);
- value: The value to be converted.
Examples
Boolean(1); // Returns true
Boolean(0); // Returns false
Boolean(""); // Returns false
7. Date.parse()
Description
The Date.parse()
function parses a string representation of a date and returns the number of milliseconds since January 1, 1970.
Syntax
Date.parse(dateString);
- dateString: The string to be parsed.
Examples
Date.parse("2024-07-14T00:00:00Z"); // Returns 1720569600000
8. JSON.parse()
Description
The JSON.parse()
function parses a JSON string and returns the corresponding JavaScript object.
Syntax
JSON.parse(text);
- text: The JSON string to be parsed.
Examples
let jsonString = '{"name":"John", "age":30}';
let obj = JSON.parse(jsonString); // Returns {name: "John", age: 30}
9. JSON.stringify()
Description
The JSON.stringify()
function converts a JavaScript object or value to a JSON string.
Syntax
JSON.stringify(value, [replacer], [space]);
value: The value to be converted.
replacer: Optional. A function or array that transforms the results.
space: Optional. A string or number that's used to insert white space into the output JSON string for readability purposes.
Examples
let obj = {name: "John", age: 30};
let jsonString = JSON.stringify(obj); // Returns '{"name":"John","age":30}'
10. Intl.NumberFormat()
Description
The Intl.NumberFormat
object is a constructor for objects that enable language-sensitive number formatting.
Syntax
new Intl.NumberFormat([locales], [options]).format(number);
locales: Optional. A string with a BCP 47 language tag, or an array of such strings.
options: Optional. An object with configuration properties.
Examples
let number = 123456.789;
let formattedNumber = new Intl.NumberFormat('en-US').format(number); // Returns "123,456.789"
11. Math.round()
, Math.floor()
, Math.ceil()
Description
These functions round numbers to the nearest integer, down to the nearest integer, or up to the nearest integer, respectively.
Syntax
Math.round(number); // Rounds to the nearest integer
Math.floor(number); // Rounds down to the nearest integer
Math.ceil(number); // Rounds up to the nearest integer
Examples
Math.round(4.5); // Returns 5
Math.floor(4.9); // Returns 4
Math.ceil(4.1); // Returns 5
Conclusion
Understanding these JavaScript conversion functions is crucial for effectively working with different data types. Whether you need to convert strings to numbers, format numbers, or handle JSON data, these functions provide the necessary tools to manipulate and utilize data in your applications. With clear examples, beginners can easily grasp how to use these functions and apply them in their coding projects.