Detailed explanation of data types in JavaScript basics

Detailed explanation of data types in JavaScript basics

1. Data Type

1.1 Why do we need data types?

In a computer, different data occupies different amounts of storage space. In order to divide data into data of different required memory sizes and make full use of the storage space, different data types are defined.

1.2 Data Types of Variables

JavaScript is a weakly typed or dynamic language, which means that there is no need to declare the data type of a variable in advance. The type will be automatically determined while the program is running. (The variable type of js is determined only when the program is running, based on the data type of the value on the right side of the equal sign)

var age = 10; //This is a numeric data type var myName = 'lili'; //This is a string data type

1.3 Data Type Classification

JS divides data types into two categories:

Simple data types (Number, String, Boolean, Undefined, Null) Complex data types (object)

2. Simple data types (basic data types)

The simple data types in JavaScript and their descriptions are as follows:

Simple data types illustrate default value
Number Numeric type, including numeric type and floating point type, such as 20, 0.12 0
Boolean Boolean types, such as true and false, are equivalent to 1 and 0 false
String String type, string with quotation marks " "
Undefined var a; variable a is declared but no value is given, so a=undefined undefined
Null var a = null; declares the variable to be null value null

2.1 Number

1. Digital system

Common bases: binary, octal, decimal, hexadecimal

Octal number sequence range: 0~7 starting with 0

Hexadecimal number sequence range: 0~9 and A~F starting with 0x

2. Digital range

Maximum and minimum values ​​of numbers in JavaScript

alert(Number.MAX_VALUE); //1.7976931348623157e+308
alert(Number.MIN_VALUE); //5e-324
  • infinity, represents infinity, greater than any data
  • -infinity, which means infinitesimal, smaller than any data
  • NaN, Not a number, represents a non-numeric value

isNaN() method is used to determine non-numbers and return a value. If it is a number, it returns false, and if it is not a number, it returns true.

2.2 String

1. String escape character

The escape characters all start with \. Commonly used escape characters and their descriptions are as follows:

Escape character explanation\n

The meaning of newline

Escape Character Explanation
\n

The meaning of newline

\\ Slash \
\' ' Single quote
\" " Double quotes
\t Tab indentation
\b Space, b is blank

2. String length

The length of the entire string can be obtained through the length property of the string

var myname = 'my name is andy';
console.log(myname.length);

2.3 Boolean

Boolean values ​​have two values: true and false, where true means true and false means false.

When adding a Boolean value to a number, true is 1 and false is 0.

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

3. Data type conversion

3.1 Convert to string

Way illustrate Case
toString() Convert to string

var num = 1;

alert(num.toString());

String() forced conversion Convert to string

var num = 1;

alert(String(num));

Plus sign concatenation string The result of concatenating with a string is a string

var num = 1;

alert(num+"I am a string");

3.2 Convert to digital type

Way illustrate Case
parseInt(string) function Convert string type to integer value parseInt('18')
parseFloat(string) function Convert string type to floating point number parseFloat('18.88')
Number() forced conversion function Convert string type to numeric type Number('18')
js implicit conversion (-*/) Using implicit arithmetic conversion to numeric type '14'-0
number() // Convert to a number number('10') // 10
number('abc') // NaN
number(true) // 1
number(false) // 0
number(null) // 0
number(undefined) // NaN
parseInt() // Convert to a number and round down // Get integers from the converted data from the front to the back. Once one is found, it will not search again. Only the code that starts with an integer will be found:
parseInt('12.345') // 12
parseInt('12abc') // 12
parseInt('abc12') // NaN
parseInt(true) // NaN
parseInt(false) // NaN
parseInt(undefined) // NaN
parseInt(null) // NaN
Note: These characters must contain numbers and start with numbers, otherwise they are all NaN
parseFloat() // Convert to number, integer, decimal code:
parseFloat('12.345') // 12.345
parseFloat('12.345abc') // 12.345
parseFloat('abc12.345') // NaN
parseFloate(true) // NaN
parseFloat(false) // NaN
parseFloat(undefined) // NaN
parseFloat(null) // NaN
Note: These characters must contain numbers and start with numbers, otherwise they are all NaN

Implicit Conversion

1. When one of the left and right sides of + is a string, the other one will be quietly converted into a string for concatenation

2. Mathematical operators convert both sides into numbers for arithmetic operations - When one of the left and right sides is a string, the + sign will concatenate them. When there is no string on either side, the + sign can also convert both sides into numbers.

3. When one of the comparison operator is a number, the other one will be quietly converted to a number for comparison.

3.3 Convert to Boolean

Values ​​that represent empty or negative values ​​will be converted to false, such as '', 0, NaN, null, and undefined. Other values ​​will be converted to true. String to Boolean type, empty string is false, and all others are true.

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • JavaScript data type conversion
  • Introduction to JavaScript basic syntax and data types
  • Eight essential data types for getting started with JS
  • Let's take a look at the most detailed explanation of JavaScript data types
  • Detailed explanation of basic data types in js
  • Eight JavaScript data types
  • Detailed explanation of the seven data types in JavaScript
  • Detailed explanation of JavaScript data types
  • Introduction to Data Types in JavaScript

<<:  MySQL msi version download and installation detailed graphic tutorial for beginners

>>:  CSS3 achieves cool 3D rotation perspective effect

Recommend

VSCode configuration Git method steps

Git is integrated in vscode, and many operations ...

This article takes you to explore NULL in MySQL

Table of contents Preface NULL in MySQL 2 NULL oc...

An article to help you understand jQuery animation

Table of contents 1. Control the display and hidi...

How does the composite index of MySQL take effect?

Table of contents background Understanding compos...

Vue implements simple calculator function

This article example shares the specific code of ...

Make your website automatically use IE7 compatibility mode when browsing IE8

Preface To help ensure that your web pages have a ...

Writing and understanding of arrow functions and this in JS

Table of contents Preface 1. How to write functio...

CSS style reset and clear (to make different browsers display the same effect)

In order to make the page display consistent betwe...

Detailed explanation of the basic implementation principle of MySQL DISTINCT

Preface DISTINCT is actually very similar to the ...

How to use Nginx to proxy multiple application sites in Docker

Preface What is the role of an agent? - Multiple ...

MySQL Server 8.0.13.0 Installation Tutorial with Pictures and Text

Install 8.0.13 based on MySQL 6.1.3. MySQL 8.0.13...

This article will show you the basics of JavaScript: deep copy and shallow copy

Table of contents Shallow copy Deep Copy Replenis...

Centos7.3 How to install and deploy Nginx and configure https

Installation Environment 1. gcc installation To i...

MySQL GROUP_CONCAT limitation solution

effect: The GROUP_CONCAT function can concatenate...

Nodejs error handling process record

This article takes the connection error ECONNREFU...