[Web front-end basics | JS basics] data types

type of data

  • Why do you need data types

In a computer, the storage space required for different data is different. In order to facilitate dividing the data into data with different required memory sizes and make full use of the storage space, different data types are defined.
Simply put, the data type is the category model of the data. For example, the name "Zhang San" and the age are 18. The types of these data are different.

One: the data type of the variable (no need to declare the type)

Variables are used to store values. They have names and data types. The data type of the variable determines how the bits representing these values ​​are stored in the computer's memory.

//int num = 10 ;    java 整型

var num;
num = 10;

//1:运行过程中才可以确定
//2:js是动态语言,变量的数据类型是可以变化的,这意味着不用提前声明变量的类型,在程序运行过程中,类型会被自动确定。

var num = 1; //数字型
num = "zhao"; //字符型

When the code is running, the data type of the variable is judged by the JS engine according to the data type of the variable value on the right side of the =. After running, the variable determines the data type.

Two: Classification of data types

1

Simple data types (basic data types)

1: Number type

JavaScript number types can be used to store integer values ​​as well as decimals (floating point numbers).

var age = 21;       // 整数
var Age = 21.3747;  // 小数     
  1. Digital base

    The most common bases are
    binary (starting with 0b), octal (starting with 0), decimal, and hexadecimal (starting with 0x).

[Talking link] Summary of the base

  1. Digital range

    Maximum and minimum values ​​in JavaScript

    alert(Number.MAX_VALUE); // 1.7976931348623157e+308
    alert(Number.MIN_VALUE); // 5e-324
    
  2. Three special numerical values

    alert(Infinity);  // Infinity
    alert(-Infinity); // -Infinity
    alert(NaN);       // NaN
    
    • Infinity, represents infinity, greater than any value

    • -Infinity, representing infinitesimal, less than any value

    • NaN, Not a number, represents a non-number

  3. isNaN

    It used to determine whether a variable is non-numeric returns true type (not a number) or a (digital) return false

[Talking] non-digital (not purely digital) ≠ digital

2: String type String

The ones placed in "" and '' are all strings;

Recommend '';

  1. String escape character

Similar to the special characters in HTML, there are special characters in strings, which we call escape characters.

The escape characters all start with \, the commonly used escape characters and their descriptions are as follows:

Escapes Explanation
\n Newline character, n means newline
\ \ Slash \
\ ’ ' apostrophe
\ " "Double quotes
\t tab indent
\b Space, black backspace
  1. String length

A string is composed of several characters, and the number of these characters is the length of the string. The length of the entire string can be obtained through the length property of the string.

Insert picture description here

  1. String splicing

Multiple strings can be spliced ​​using +. The splicing method is
◮string + any type = new string after
splicing, before splicing any type added to the string into a string, and then splicing into one New string

//1.1 字符串 "相加"
 alert('hello' + ' ' + 'world'); // hello world
//1.2 数值字符串 "相加"
 alert('100' + '100'); // 100100
 //1.3 数值字符串 + 数值
 alert('11' + 12);     // 1112 
  1. String splicing enhancement
    • Strings and variables are often spliced ​​together. Variables can easily modify the values ​​inside
    • Variables cannot be quoted, because quoted variables will become strings
    • If there are string concatenations on both sides of the variable, the formula "quote plus plus", delete the number, and write the variable plus the middle

3: Boolean

The boolean type has two values: true and false, where true means true (right) and false means false (false).

When adding a Boolean and a numeric type, the value of true is 1 and the value of false is 0.

console.log(true + 1);  // 2
console.log(false + 1); // 1

4:Undefined

A variable that has not been assigned after declaration will have a default value of undefined (if connecting or adding, pay attention to the result)

  var variable;
  console.log(variable);           // undefined
  console.log('你好' + variable);  // 你好undefined
  console.log(11 + variable);     // NaN
  console.log(true + variable);   //  NaN

5: Null

One declares a variable to give a null value, and the value stored in it is empty (when learning objects, we will continue to study null)

  var vari = null;
  console.log('你好' + vari);  // 你好null
  console.log(11 + vari);     // 11
  console.log(true + vari);   //  1

Three: Get variable data type

  1. (Typeof)_keyword
    2
  2. Literal: Format characteristics of data

A literal is a representation of a fixed value in the source code. In layman's terms, it means how to express this value.

3

Four: data type conversion

What is data type conversion?
The data obtained by using forms and prompts is of string type by default. In this case, you cannot simply perform addition operations directly, but need to convert the data type of the variable. Generally speaking, it is to convert a variable of one data type into another data type.

-Convert to string

method Example (var num = 1)
console.log(num.toString());
String() coercion console.log(String(num));
Plus sign concatenation string console.log(num +''); implicit conversion

-Convert to digital type (emphasis)

method For example
parseInt(string) function Integer parseInt(‘78’);
parseFloat(string) function Floating point parseFloat(‘78.12’);
Number() casting function Number(‘12’);
js implicit conversion (-0; *1; /1) ‘12’-0

【叽叽叽溫溫】

  1. Case of parseInt and parseFloat words
  2. Implicit conversion is when we are performing arithmetic operations, JS automatically converts the data type

-Convert to Boolean

Boolean(数据);

//转换为布尔值为false的数据
//代表空、否定的值会被转换为 false 
''0NaNnull、undefined 
// 其余值都会被转换为 true

console.log(Boolean('')); // false
console.log(Boolean(0)); // false
console.log(Boolean(NaN)); // false
console.log(Boolean(null)); // false
console.log(Boolean(undefined)); // false
console.log(Boolean('小白')); // true
console.log(Boolean(12)); // true

-Object, string conversion

Object to string: JSON.stringify(obj);
String to object: JSON.parse(str);

Five: interpreted language and compiled language

4
5

Guess you like

Origin blog.csdn.net/qq_43490212/article/details/111174612