You are currently viewing JavaScript Type Conversions and Equality

JavaScript Type Conversions and Equality

JavaScript Type Conversions and Equality
JavaScript Type Conversions and Equality

JavaScript type conversion is the method of converting one type of data to another type.

Types of JavaScript Type Conversion

  • Explicit Type Conversion: JavaScript converts the data with the help of a function. It is a manual process.
  • Implicit Type Conversion: JavaScript itself converts the data. It is an Automatic process.

Explicit Conversion

This conversion is done with the help of a built-in function.

  • Convert to Number
  • Convert to String
  • Convert to Boolean

Convert to Number

We use of Number() method for this conversion. Boolean, String, and Dates can easily be converted to Numeric values.

//Conversion of String , Boolean values to Numeric values.

let a = "JavaScript";
console.log(a);//JavaScript
a = Number(a);
console.log(a);//NaN

let b = '12';
console.log(b);//12
b = Number(b);
console.log(b);//12

let c = true;
c = Number(c);
console.log(c);//1

let d = false;
d = Number(d);
console.log(d);//0

Convert to String

We use of String() and toString() method for the conversion of data types to String.

//Conversion of Number, Boolean, null  values to String.

let a = 12
console.log(a);//12
a = String(a);
console.log(a);//12

let b = true;
b = String(b);
console.log(b);//"true"
console.log(typeof(b));//string

let c = String(null);
console.log(c);//"null"
console.log(typeof(c));//string

Convert to Boolean

We use of Boolean() method for the conversion of data types to Boolean values.

//Conversion of Number, String, null , undefined to Boolean.

let a = Boolean('s');
console.log(a); // true

let b  = Boolean(1);
console.log(b); // false

let c  = Boolean(undefined);
console.log(c); // false

let d = Boolean(null);
console.log(d); // false

let e  = Boolean(NaN);
console.log(e); // false

Implicit Conversion

Implicit conversion is an automatic process. JavaScript itself converts one data type to another.

Example:

Convert to Number.[Implicit conversion]

//Convert to Number
let num;
num = '20' - 10;
console.log(num); // 10

num = '20' - '10'; 
console.log(num); // 10

num = '20' / 10;
console.log(num); // 2

num = '20' * 10;
console.log(num); // 200

convert to String.[Implicit conversion]



//Convert to String
let a = '1' + 2; 
console.log(a) // "12" because 2 is converted to '2'

let b = 'ab' + true; 
console.log(b); // "abtrue" because true is converted to "true"

let c = '22' + null; 
console.log(c); // "22null" because null is converted to "null"

let d = '22' + undefined; 
console.log(d); // "22undefined" because undefined is converted to "undefined"

convert to Boolean.[Implicit conversion]

let bool;

bool = '0' - true;
console.log(bool); // -1

bool = '0' - false;
console.log(bool); // 0

bool = '1' - true;
console.log(bool); // 0

bool = 1 + true;
console.log(bool); // 2

bool = 0 + false;
console.log(bool); // 0

When we use – , / , * operation with non-numeric string this yields to NaN.[Implicit conversion]

// when we use - , / , * operation with non-numeric string this yields to NaN.
let val;
val = 'JavaScript' - 'Code';
console.log(val); // NaN

val = '0' - 'h';
console.log(val); // NaN

Equality

The Equality Operators are == and !=. These operators Compare two values and check whether they are equal or not. And it returns Boolean Value (Either true or false). Also, we have a Strictly Equal Operator ===.

.Equal to (==) Operator

Equal to operator check for the equality of two values. If two values are equal then it returns a true value. If two values are not equal then it returns false.

console.log(1 == 1);//  true

console.log('Hello' == 'Hello');//  true

console.log(1 == true);// true

console.log(0 == false);// true

console.log('1' ==  1);// true

Not Equal to (!=) Operator

Not Equal to operators compare two values If both values are equal then it returns false and If both values are not equal it returns true.

console.log(1 != 2);//  true

console.log('Hello' != 'Hello');//  false

console.log('Hello' != 'hello');// true

console.log(1 != true);// false

console.log(1 != false);// true

console.log('1' !=  1);// false

Comparison of Object literals in JavaScript

//Comparison of Object literals.
const ob1 = {
    "1": "abc",
    "2":"xyz"
    
};

const ob2 = {
    "1": "abc",
    "2":"xyz"
    
};

console.log(ob1 == ob1) // true
console.log(ob1 == ob2) // false

Comparison of String and Arrays in JavaScript

//comparison of String and Array in JS.
const a = [0, 5, 10, 15];
const b = "0,5,10,15";
console.log(a == b); // true because a gets converted to String [Implicit Conversion]

Strictly Equal === Operator

Also, explain in JavaScript Operators. It is also known as is exactly equal to operator. If the two values(operands) are of the same data type then they are considered to be equal.

// Strictly Equal operator
console.log(1 === 1); // true
console.log(2 === '2'); // false

Check all previous tutorial JavaScript.

Written By : Ankita Kataria.

Want to learn about DevOps technology then you must check this DevOps Beginner Guide.

Leave a Reply