JavaScript - 04 data type conversion

Posted by zkoneffko on Fri, 17 Dec 2021 06:58:07 +0100

19. Cast type

Cast, also known as explicit cast

19.1 # cast to number # 3 methods

▶ Convert content to numbers

// Usage: Number(a)

// If the content can be converted to a number, the corresponding number (decimal or integer) is returned
console.log(Number('1.01'));  // 1.01

// If the content cannot be converted to numbers, NaN is returned
console.log(Number('abc'));  // NaN

// If the content is empty, 0 is returned
console.log(Number(''));  // 0

// In memory, boolean type is stored in digital class, true = 1, false = 0
console.log(Number(true), Number(false));  // 1 0

▶ Convert contents to integers (remove decimals without rounding)

Parsing starts from the first character until it encounters a non numeric symbol, stops, and returns part of the parsed value

// Usage: parseInt(a)
console.log(parseInt("22.9A"));  // 22

▶ Convert content to decimal

Parsing starts from the first character until it encounters a non numeric symbol, stops, and returns part of the parsed value

// Usage: parseFloat(a)
console.log(parseFloat("22.9876A"));  // 22.9876

19.2 , cast to string , 3 methods

▶ Concatenate empty strings

// Usage: '' + content to convert
console.log(typeof('' + 10));  // string

▶ Use String() to put the content to be converted in parentheses after String

// Usage: String(a)
console.log(typeof String(10));  // string

▶ Call toString() directly

// Usage: toString()
var num = 10;
console.log(typeof num.toString());  // string

19.3 , cast to boolean , 2 methods

  • False, 0, empty string, Undefined, null, NaN will be converted to false
  • Everything else will be converted to true

▶ Convert using Boolean()

// Usage: Boolean(a)
console.log(Boolean(false), Boolean(0), Boolean(""), Boolean(undefined), Boolean(null), Boolean(NaN));  // false false false false false false
console.log(Boolean("a"), Boolean(1), Boolean(true));  // true true true

▶ use!! Conversion (reverse and reverse)

// Usage:!! a
console.log(!!false, !!0, !!"", !!undefined, !!null, !!NaN);  // false false false false false false
console.log(!!"a", !!1, !!true);  // true true true

20. Automatic type conversion

Automatic type conversion, also known as implicit conversion

Automatic conversion to number type} 2 methods

▶ Method 1: it will be automatically converted to number when participating in operations such as - * /%

console.log("10"-1, 2*"2", "4"/2, 10%"3");  // 9 4 2 1
var num = prompt("Please enter a number:")*1;  // *1 = automatic type conversion occurs

▶ Method 2: add directly before the content to be converted+

// Method 2: add directly before the content to be converted+
console.log(+"10" + 2);  // 12

21. Data type conversion exercise

21.1} exercise 1

Prompt the user to enter a positive integer greater than 50, then judge whether the user's input meets the requirements, and prompt:

var num = prompt("Please enter a positive integer greater than 50:");
var result = isNaN(num*1) || num%1!=0 || num*1<=50 ? "It doesn't meet the requirements. What you entered is:" + num : "Yes, you entered:" + num;
console.log(result);

21.2} exercise 2

Prompt the user to enter the length and width of the rectangle, and calculate the perimeter and area of the rectangle:

var length = prompt("Please enter the length of the rectangle:");
var width = prompt("Please enter the width of the rectangle:");
var girth = (length*1 + width*1)*2;  // Rectangle perimeter = (length + width) * 2
var area = length * width;  // Rectangular area = length * width
console.log("The perimeter of the rectangle is:" + girth + ",Area:" + area);

21.3} exercise 3

Accept the positive integer entered by the user, judge whether it is a multiple of 5, and output the corresponding results:

var num = prompt("Please enter a positive integer:");
var result = num%5 == 0 ? num + "Is a multiple of 5" : num + "Not a multiple of 5";
console.log(result);

21.4} exercise 4

Receive the scores of two students, compare their scores, and output the score difference:

var student1 = prompt("Please enter the grade of the first student:");
var student2 = prompt("Please enter the grade of the second student:");
console.log("Score difference:" + Math.abs(student1-student2));

21.5} exercise 5

Prompt the user to enter a three digit number, such as 723, and output it in reverse order 327:

var num = prompt("Please enter a three digit number:");
var a = Math.floor(num/100);  // Take out the first digit
var b = Math.floor((num/10)%10);  // Take out the second digit
var c = Math.floor(num%10);  // Take out the third digit
console.log("" + c + b + a);

Topics: Javascript