KandZ – Tuts

We like to help…!

JavaScript 7 🧬 Automatic type conversion and typeof operator

a - Automatic data type conversion
JavaScript automatically converts data types between strings, numbers and boolean
This process is also known as implicit type conversion
console.log(1 + '2'); → automatically convert 1 to string, result: 12
console.log('10' - '4'); → both strings are converted to numbers, result 4
console.log('10' + 4); → result 104
console.log(5 * null) → null becomes 0, result 0
console.log(5 * true) → true becomes 1, result 5
console.log(5 * false) → false become 0, result 0

b - typeof operator
typeof operator is used to determine the type of a variable or value.
It returns a string indicating the type of the unevaluated operand.
This can be particularly useful for debugging and ensuring that variables are of the expected types.
console.log(typeof 42); → result: number
console.log(typeof ""); → result: string
console.log(typeof true); → result: boolean
declared variable example → result: undefined
object example → result: object
function example → result: function

Leave a Reply