1. LiteralsA representation used to express a fixed value, also called a constant. 1.1 Numeric literals<script> // integer literal // decimal console.log(12); // Octal console.log(010); // Hexadecimal console.log(0x100); </script Effect display 1.2 Floating-point literalsFloating-point numbers do not distinguish between bases, all floating-point numbers are in decimal (Note: if the floating-point number is between 0 and 1, the leading 0 can be omitted, for example, 0.6 can be written as .6). The precision of floating-point numbers is far less than that of decimals. // Floating point literal console.log(1.2); console.log(-1.2); console.log(0.1343); console.log(.1343); console.log(1.2e4); console.log(1.2e-4); Effect display 1.3 Special ValuesInfinity // Infinity console.log(Number.MAX_VALUE); console.log(Number.MIN_VALUE); console.log(5e789); console.log(-5e789); console.log(Infinity); Effect display NaN : Not a normal number isNaN() determines whether a data is NaN. // NaN console.log(0 / 0); console.log(isNaN(0 / 0)); console.log(isNaN(22)); Effect display 1.4 String literalsString literals : Wrap it in single or double quotes. There can be any number of characters between the quotes, or an empty string if there are no characters. (Note: If the string contains double quotes, it should be wrapped in single quotes, and vice versa) // string literal console.log("This is a 'string'"); console.log("123"); console.log("000%$*^"); console.log(" "); console.log(""); Effect display **Escape character**: // escape character console.log("hello"); console.log("Hello"); console.log("Hello\"ya\""); Effect display Variables2.1 Definition of variablesVariable: An identifier for data stored in a computer. The data in the memory can be obtained based on the variable name. Variable declaration: Define and create variables before using them. If a variable is not defined before being used, a reference error will occur. Definition method: add a space after var, and then add a variable name after the space. // variable definition var name; var userName; var a; var b1; 2.2 Variable AssignmentAfter a variable is defined, it has a default value of undefined; when a variable is assigned a value, any type of data can be stored internally, even a variable. A var can define multiple variables at the same time, separated by commas. The code is as follows (example): // variable definition var name; var userName; var a; var b1; // variable assignment var a=1,b=2,c=3; a = a + b + c; console.log(a); Effect display: 3. Data Type3.1 Simple Data TypesComplex data type object. 3.2 Detecting data types Use typeof to detect the data type, add parentheses after it, and write the parameters in the parentheses. (The data type of a variable is variable, and the data type will be different as the value is assigned) console.log(typeof(6.8)); console.log(typeof(-1)); console.log(typeof(NaN)); console.log(typeof(false)); console.log(typeof 66); console.log(typeof 66 + 3); Effect display 3.3 Data Type Conversion(1) Convert to a string toString() and String() convert data into string type. The special feature of "+" is that as long as there is a string on both sides, it can concatenate strings. If there are numbers on both sides, then it is an arithmetic function. (2) Convert to digital type Number() can convert other types of data into numeric types. parseInt() method to convert a string into an integer: floating point numbers can be rounded to convert the string into an integer. (for numeric strings) (3) Convert to Boolean type Boolean() can convert any type of data and convert other data into Boolean values. Converts to false: NaN, 0, "", empty string, null, undefined Convert to true: non-0, non-NaN number, non-empty string // Convert to string type console.log(true.toString()); console.log(String(2333)); console.log(1 + ""); //Convert to digital type console.log(Number("11aa")); console.log(parseInt("123.11ac")); //Convert to integer console.log(parseFloat("12.123hh")); // Convert to Boolean type console.log(Boolean(NaN)); console.log(Boolean("")); console.log(Boolean(undefined)); console.log(Boolean(" ")); console.log(Boolean(123)); 3.4 prompt receives data conversion// var num = prompt("Please enter a number within ten"); // num = parseInt(num); // console.log(typeof(num)); // Simplified writing var num = parseInt(prompt("Please enter a number within ten")); num = num + 3; console.log(num); IV. ConclusionThis is the end of our study on data types. Keep learning the front end and hope to see a different self. This is the end of this article about the detailed knowledge of JavaScript data types. For more relevant js data types, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: The simplest MySQL data backup and restore tutorial in history (Part 2) (Part 37)
>>: How to deploy MySQL 5.7 & 8.0 master-slave cluster using Docker
After clicking the a tag in the page, you want to ...
Table of contents Overview How to share data betw...
Table of contents Hidden, descending, and functio...
There are probably as many modular solutions for ...
Summary This article will introduce the following...
1. Purpose Write a Flask application locally, pac...
1. Preparation before installation 1. Download th...
Preface The writing of front-end code can never e...
Before the release of Microsoft IE 5.0, the bigges...
Table of contents 1. Brief Overview 2. Detailed e...
I used the label tag when I was doing something re...
This article example shares the specific code for...
Be careful when listening for events that are tri...
Today, when installing nginx on the cloud server,...
Table of contents The browser's rendering mec...