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

Vue integrates a rich text editor that supports image zooming and dragging

need: According to business requirements, it is n...

MySQL database case sensitivity issue

In MySQL, databases correspond to directories wit...

MySQL 5.6.28 installation and configuration tutorial under Linux (Ubuntu)

mysql5.6.28 installation and configuration method...

CocosCreator learning modular script

Cocos Creator modular script Cocos Creator allows...

Quickly learn MySQL basics

Table of contents Understanding SQL Understanding...

mysql solves time zone related problems

Preface: When using MySQL, you may encounter time...

React implements the addition, deletion, modification and query of todolist

Table of contents Take todolist as an example The...

How to implement the strategy pattern in Javascript

Table of contents Overview Code Implementation Su...

VMware Workstation 14 Pro installs CentOS 7.0

The specific method of installing CentOS 7.0 on V...

How to install the graphical interface in Linux

1. Linux installation (root user operation) 1. In...

Let IE6, IE7, IE8 support CSS3 rounded corners and shadow styles

I want to make a page using CSS3 rounded corners ...

Tips for creating two-dimensional arrays in JavaScript

Creation of a two-dimensional array in Js: First ...

Detailed explanation of the 4 codes that turn the website black, white and gray

The 2008.5.12 Wenchuan earthquake in Sichuan took...

Summary of 6 solutions for implementing singleton mode in JS

Preface Today, I was reviewing the creational pat...