How to analyze Javascript type conversion
Today, I will talk to you about how to analyze Javascript type conversion. Many people may not know much about it. In order to let you know more, Xiaobian summarizes the following contents for you. I hope you can gain something according to this article.
JS is short for JavaScript, it is a literal script language, its interpreter is called JavaScript engine, is part of the browser, mainly used for web development, can add a variety of dynamic effects to the website, make the page more beautiful.
Type conversion 1.String->Numberconst string = "99";//Implicit conversion console.log (string * 1 + 77);//Explicit conversion console.log (Number(string) + 77);
2.Number->Stringconst number = 66; console.log (typeof number);//implicitly convert console.log (typeof (number + ""));//explicitly convert console.log (typeof String(number));
3. Mixed String->Number
Convert the numeric string at the beginning to numeric
const string = "66.66alison";console.log(parseInt(string));console.log(parseFloat(string));
4.String->Arrayconst name = "alison";console.log(name.split(""));
5.Array->Stringconst array = ["yooo", "alison", "ruby", "andy"];console.log(array.join(""));console.log(array.join("&"));
6.Array->Number
0 for empty arrays, x for single-element arrays, NaN for multi-element arrays
console.log(Number([]));console.log(Number([3]));console.log(Number([1, 2, 3]));
7.toString()
It is mainly used to convert Array (separated by commas), Boolean, Date, Number and other objects into String.
const array = ["yooo", "alison", "ruby", "andy"];console.log(array.toString());const number = 99;console.log(typeof number.toString());
8. Boolean implicit conversion
Comparison and arithmetic are two things.
1) Comparison
When comparing boolean with number or string, it implicitly turns true to 1 and false to 0 in boolean.
let number = 99;console.log(number == true);
let hd = '0';let hd2 = "1";let hd3 = "99";console.log(hd == false);console.log(hd2 == true);console.log(hd3 == true);
(2) Operation
string and number are implicitly converted to boolean in arithmetic
let number = 99;if (number) console.log("number");
(3) Other types are converted to Booleanconsole.log (Boolean([])); console.log (Boolean({}));
False True Numeric Type 0 Other String Type Empty String Other Reference Type Arrays and Objects 9. Boolean Explicit Conversion (1)!!
! Convert number to boolean, then negate
!! It's equivalent to turning numbers into booleans.
(2) constructor Boolean()//numeric let number = 0;number = !! number;console.log(Boolean(number));//string let string = "Alison";console.log(!! string);console.log(Boolean(string));//array let array = [];console.log(!! array);console.log(Boolean(array));//object let object = {};console.log(!! object);console.log(Boolean(object));//date let date = new Date();console.log(!! date);console.log(Boolean(date)); After reading the above, do you have any further understanding of how to parse Javascript type conversions? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.