Posts

Showing posts from March, 2022

JavaScript - Implict null to number conversion

// null is 0 when used with number let num; num = 4 + null; console.log(num); // 4 num = 4 - null; console.log(num); // 4

JavaScript Implicit Boolean Conversion to Number

In Boolean true is 1, false is 0 let num; num = '4' - true; console.log(num); // 3 num = 4 + true; console.log(num); // 5 num = 4 + false; console.log(num); // 4

JavaScript implict conversion to number

You can implict convert a string to number using the following operators - , / , * let num; num = '6' - '4'; console.log(num); // 2 num = '4' - 2; console.log(num); // 2 num = '4' * 2; console.log(num); // 8 num = '4' / 2; console.log(num); // 2

JavaScript Implict Conversion to String

Implicit conversion is when JavaScript automatically converts one data type to another. let str; str = '1' + 2; console.log(str) // "12" str = '1' + true; console.log(str); // "1true" str = '1' + undefined; console.log(str); // "1undefined" str = '1' + null; console.log(str); // "1null"

Swapping array elements in JavaScript

Elements can be swapped using destructuring assignment expression var numbers = [1,2,3]; [numbers[2], numbers[1]] = [numbers[1], numbers[2]]; console.log(numbers); // [1,3,2] Without destructuring assignment, swapping two values requires a temporary variable