Data type is a kind or form of value store in a declare variable. Is the value store a number or string?
Types of data type:
a. Primitive data types
b. Non primitives data types
Primitive Data-types
Number
String
Boolean
Undefined
Null
BigInt
Symbol
Non-Primitive Data-types
Object
Array
RegExp
JavaScript has some in built conversion one can explore without the use of methods:
let result;
//For example =>
// String conversion
result = '3' + 2; // "32"
result = '10' + true; //"10true"
//Number conversion
result = '10' - '2'; // 8
result = '10' - 2; //8
//NaN conversion
result = 'hello' - 'world'; // NaN
result = '4' - 'hello'; //NaN
//Boolean conversion
result = '8' - true; // 7
result = 4 + true; //5
//Null conversion
result = 15 + null; // 15
result = 4 - null; //4
To determine types of data type use, the keyword typeof
helps check.
console.log(typeof(num)); //Number
Modification of data types (converting)
Changing a value stored from one data types to another
Changing value of the declare variable to a different data types can be done by ensuring that the data type being converted to, is the one use and begins with a Capital letter. Clear example:
let year = String(1980);
console.log(year); //"1980"
This value was a number data type and then converted to a string data type using String
Generally:
let x = Data-types(value);
convert to number
A string value change to number type by using Number
keyword, as well as some data types such as boolean and null in JavaScript.
For example: true
is known as 1
while false
, empty string ""
and null
is 0
or empty value.
A text, NaN
and undefined
will return Not a Number NaN
Some method use in Number type
some specific method can be used to convert one data types to a number. Method like:
1. parseInt()
2. parseFloat()
3. /*unary operator*/ +""
4. Math.floor()
example:
convert to string
conversion to string is done with String /*or*/ toString()
. example:
convert to boolean
empty string
, 0
, undefined
, NaN
and null
return false
when passed with boolean but others return true
. example:
Double Not !!
can be use as boolean converter
console.log(!!1); //true
console.log(!!0); //false
console.log(!!null); //false
More article on each of this data types both primitive and non-primitive types coming soon...
Let us connect with each other
connection between two or more people bring solution to a problem...