Detailed explanation of JavaScript data types

Detailed explanation of JavaScript data types

1. Literals

A representation used to express a fixed value, also called a constant.

1.1 Numeric literals

insert image description here

<script>
    // integer literal // decimal console.log(12);
    // Octal console.log(010);
    // Hexadecimal console.log(0x100);
</script

Effect display

insert image description here

1.2 Floating-point literals

Floating-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

insert image description here

1.3 Special Values

Infinity

// Infinity
console.log(Number.MAX_VALUE);
console.log(Number.MIN_VALUE);
console.log(5e789);
console.log(-5e789);
console.log(Infinity);

Effect display

insert image description here

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

insert image description here

1.4 String literals

String 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

insert image description here

**Escape character**:

insert image description here

// escape character console.log("hello");
console.log("Hello");
console.log("Hello\"ya\"");

Effect display

insert image description here

Variables

2.1 Definition of variables

Variable: 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.

insert image description here

// variable definition var name;
var userName;
var a;
var b1;

2.2 Variable Assignment

After 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:

insert image description here

3. Data Type

3.1 Simple Data Types

insert image description here

Complex 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)
The code is as follows (example):

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

insert image description here

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)
parseFloat() converts to floating point number.

(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. Conclusion

This is the end of our study on data types. Keep learning the front end and hope to see a different self.

insert image description here

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:
  • 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 data types in JavaScript basics
  • Introduction to Data Types in JavaScript

<<:  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

Recommend

Use href to simply click on a link to jump to a specified place on the page

After clicking the a tag in the page, you want to ...

In-depth understanding of the role of Vuex

Table of contents Overview How to share data betw...

The three new indexes added in MySQL 8 are hidden, descending, and functions

Table of contents Hidden, descending, and functio...

CSS modular solution

There are probably as many modular solutions for ...

Several common CSS layouts (summary)

Summary This article will introduce the following...

Docker deployment of Flask application implementation steps

1. Purpose Write a Flask application locally, pac...

VMware15.5 installation Ubuntu20.04 graphic tutorial

1. Preparation before installation 1. Download th...

A brief discussion on mobile terminal adaptation

Preface The writing of front-end code can never e...

Overview of the definition of HTC components after IE5.0

Before the release of Microsoft IE 5.0, the bigges...

Detailed explanation of MySql data type tutorial examples

Table of contents 1. Brief Overview 2. Detailed e...

Analysis and solutions to problems encountered in the use of label tags

I used the label tag when I was doing something re...

Vue shuttle box realizes up and down movement

This article example shares the specific code for...

Analysis of examples of using anti-shake and throttling in Vue components

Be careful when listening for events that are tri...

Solve the problem of yum installation error Protected multilib versions

Today, when installing nginx on the cloud server,...