JavaScript data type and conversion -- JS

catalogue

1. Data type

1.1 why do you need data types

Data type of variable 1.2

1.3 data type classification

2. Template string (newly added in ES6) -- template literal

2.1 analytic variables

2.2 string can wrap

2.3 calling function

3.typeof and literal quantity

3.1 use of typeof

3.2 literal quantity

4. Data type conversion

4.1 what is a data type

4.2 convert to string type

4.3 converting to digital

4.4 convert to Boolean

1. Data type

1.1 why do you need data types

Because the storage space occupied by different data is different, in order to facilitate the data to be divided into data with different memory sizes, make full use of the storage space.

1.2 data types of variables

JS is a weak type or dynamic language, which will automatically determine the type during operation,

var x=123;

var x=‘syh’;// The type can vary

1.3 data type classification

1. Simple data type

2. Complex data types

Simple data typeexplainDefault value
NumberDigital, 21,0.210
BooleanBoolean value types, such as true and false

fales 0

ture 1

StringString type ''"   "
undefinedvar a; Undefinedundefined
Nullvar a=NULL; A is nullnull

be careful:

    //1. Octal
    var num=010;// The first 0 in the front represents octal
    console.log(num);
    //2. Hexadecimal
    var num1=0x9;// 0x indicates hexadecimal
    console.log(num);
    
    //3. Digital Number max min
    alert(Number.MAX_VALUE);
    alert(Number.MIN_VALUE);

    //Three special values
    alert(Number.MAX_VALUE*2);//Infinity
    alert(Number.MIN_VALUE*2);//-Infinity
    alert('pink teacher'-100);//NAN is not a numeric value. If it is a number, it returns false. If not, it returns true
    //isNAN() judge non numeric
    // console.log(is NAN(21));  Return false

    //4. string type
    var str="I am a'Math is not good'of\n programmer";
    console.log(str);//I'm a 'bad Math' student
                     //programmer
//String escape characters should be written in quotation marks
    //    \'single quotation mark \ "double quotation mark \ t tab \ B space 
    //Find string length
    var str="my name is andy";
    console.log(str.length);//15

    //String splicing
    console.log('desert'+'camel');//Desert camel 
    console.log('pink'+18);//pink
    console.log('pink'+ture);//pinkture
    console.log(12+12);//24
    console.log('12'+12);//'1212'
    //As long as there are strings and other types, the splicing result is of string type
//6.undefined and NULL
    var variable;
    console.log(variable);//undefined
    console.log('Hello'+variable);//Hello, variable
    console.log(11+variable);//NaN
    console.log(ture+variable);//NaN

    var variable=null;
    console.log('Hello'+variable);//Hello, null
    console.log(11+variable);//11
    console.log(ture+variable);//1

2. Template string (newly added in ES6) -- template literal

2.1 analytic variables

let name='Zhang San';
let sayHello='Hello,My name is ${name}';
console.log(sayHello);

2.2 string can wrap

let result ={
    name:"ZHANGSAN",
    age:20
};
let html =
<div>

    <span>${result.name}</span>
    <span>${result.age}</span>

</div>
;
     console.log(html);

2.3 calling function

const fn=()=>{
        return 'I am fn function'
}
let html ='I am a template string ${fn()}'
        console.log(html);

3.typeof and literal quantity

3.1 use of typeof

Expansion:

The prompt is of character type

3.2 literal quantity

Literal is the representation of a fixed value in the source code

Number literal: 8,9,10

String literal: 'Black Friday'

Boolean literal: true false

4. Data type conversion

4.1 what is a data type

Using the form (input), the data obtained by prompt is of string type by default. At this time, addition operation cannot be carried out directly. Character conversion is required, that is, to convert variables of one data type into another data type

There are usually three types

1) Convert to string type

2) Convert to digital

3) Convert to Boolean

4.2 convert to string type

modeexplain
Variable tostring ()Convert to string
string castConvert to string
Plus concatenated stringConcatenation and string result

 

4.3 converting to digital

modeexplaincase
parseInt (string) functionConvert string to numericparseInt('78')//78
parseFloat(string) functionConvert string to floating point numberparseFloat('78.82')//78.82
Number() castConvert string to numericNumber('12')//12
Implicit conversion (- + /)Implicit conversion to numerical type by arithmetic operation'12'-0

4.4 convert to Boolean

modeexplaincase
Boolean() functionConvert other types to BooleansBoolean('1')//ture

Tags: Java Javascript Visual Studio Code

Posted by kevin7 on Sun, 10 Apr 2022 21:40:50 +0930