Commonly used data types in JavaScript are: 1. null type, which represents a null value and defines a null object pointer; 2. undefined type, which represents an undefined value; 3. number type; 4. string type; 5 , boolean type; 6. object type; 7. Array type.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Commonly used data types in javascript
Data types | Description |
---|---|
null | Null value means non-object |
undefined | Undefined value means unassigned initialization value |
number | Number, the value of mathematical operation |
string | String, representing the flow of information |
boolean | Boolean value, the value of logical operation |
object | Object, representing a data set of a composite structure |
Array | Array, containing an ordered collection of encoded values. |
1. Null
The Null type has only one value, null, which represents a null value and defines a null object pointer.
Use the typeof operator to detect null values and return Object, indicating that it belongs to the object type, but JavaScript classifies it as a special type of value.
Set the initialization value of the variable to null, and you can define a spare empty object, which is a special object value, or a non-object. For example, if you detect that an object is empty, you can initialize it.
if (men == null){ men = { //初始化men } }
2. Undefined type
undefined is the only value of the Undefined type, which represents an undefined value. When a variable is declared without a value assigned, or a property is defined without a value set, the default value is undefined.
Example 1
undefined is derived from null. Both null and undefined represent empty values. When converted into Boolean values, they are both false values and can be equal.
console.log(null == undefined); //返回 true
null and undefined belong to two different types, which can be detected using the equality operator (==) or the typeof operator.
console.log(null === undefined); //false console.log(typeof null); //返回"object" console.log(typeof undefined); //返回"undefined"
Example 2
To check whether a variable is initialized, you can use undefined to quickly detect.
var a; //声明变量 console.log(a); //返回变量默认值为 undefined (a == undefined) && (a = 0); //检测变量是否初始化,否则为其赋值 console.log(a); //返回初始值 0
You can also use the typeof operator to detect whether the type of a variable is undefined.
(typeof a == "undefined") && (a = 0); //检测变量是否初始化,否则为其赋值
3. Boolean type
Boolean type (Boolean) contains only two fixed values: true and false. Among them, true represents "true" and false represents "false".
In JavaScript, the six special values of undefined, null, "", 0, NaN and false are false when converted to Boolean values, which are called false values. Except for false values, any other type of data converted to a Boolean value is true.
Example
Use the Boolean() function to force a value to a Boolean value.
console.log(Boolean(0)); //返回 false console.log(Boolean(NaN)); //返回 false console.log(Boolean(null)); //返回 false console.log(Boolean("")); //返回 false console.log(Boolean(undefined)); //返回 false
4. Number:
There are two forms of representation of this type, the first is an integer, and the second is a floating point number. Integer: can be represented by decimal, octal, and hexadecimal literal values. Floating point number: The value must contain a decimal point and there must be one digit after the decimal point.
5. String:
The String type is used to represent a character sequence consisting of zero or more 16-bit Unicode characters, that is, a string. As for whether to use single quotes or double quotes, there is still no difference in js. Remember to come in pairs.
6. Object:
The Object data type, called an object, is a collection of data and functions (functions). It can be created using the new operator followed by the name of the object type to be created. Can also be created using literal notation. Add a property with a different name (any string including the empty string).
7. Array
JavaScript arrays are written in square brackets. The items of the array are separated by commas.
The following code declares (creates) an array named cars, containing three items (car brands):
var cars = ["Porsche", "Volvo", "BMW"];
The array index is based on zero, which means that the first item is [ 0], the second item is [1], and so on.
Arrays in ECMAScript are quite different from arrays in other languages:
Each item in an ECMAScript array can save any type of data;
The size of the ECMAScript array can be dynamically adjusted, and elements can be added or deleted from the array;
[Related recommendations: javascript learning tutorial 】
The above is the detailed content of What are the commonly used data types in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Classselectorsareversatileandreusable,whileidselectorsareuniqueandspecific.1)Useclassselectors(denotedby.)forstylingmultipleelementswithsharedcharacteristics.2)Useidselectors(denotedby#)forstylinguniqueelementsonapage.Classselectorsoffermoreflexibili

IDsareuniqueidentifiersforsingleelements,whileclassesstylemultipleelements.1)UseIDsforuniqueelementsandJavaScripthooks.2)Useclassesforreusable,flexiblestylingacrossmultipleelements.

Using a class-only selector can improve code reusability and maintainability, but requires managing class names and priorities. 1. Improve reusability and flexibility, 2. Combining multiple classes to create complex styles, 3. It may lead to lengthy class names and priorities, 4. The performance impact is small, 5. Follow best practices such as concise naming and usage conventions.

ID and class selectors are used in CSS for unique and multi-element style settings respectively. 1. The ID selector (#) is suitable for a single element, such as a specific navigation menu. 2.Class selector (.) is used for multiple elements, such as unified button style. IDs should be used with caution, avoid excessive specificity, and prioritize class for improved style reusability and flexibility.

Key goals and advantages of HTML5 include: 1) Enhanced web semantic structure, 2) Improved multimedia support, and 3) Promoting cross-platform compatibility. These goals lead to better accessibility, richer user experience and more efficient development processes.

The goal of HTML5 is to simplify the development process, improve user experience, and ensure the dynamic and accessible network. 1) Simplify the development of multimedia content by natively supporting audio and video elements; 2) Introduce semantic elements such as, etc. to improve content structure and SEO friendliness; 3) Enhance offline functions through application cache; 4) Use elements to improve page interactivity; 5) Optimize mobile compatibility and support responsive design; 6) Improve form functions and simplify verification process; 7) Provide performance optimization tools such as async and defer attributes.

HTML5transformswebdevelopmentbyintroducingsemanticelements,multimediacapabilities,powerfulAPIs,andperformanceoptimizationtools.1)Semanticelementslike,,,andenhanceSEOandaccessibility.2)Multimediaelementsandallowdirectembeddingwithoutplugins,improvingu

TherealdifferencebetweenusinganIDversusaclassinCSSisthatIDsareuniqueandhavehigherspecificity,whileclassesarereusableandbetterforstylingmultipleelements.UseIDsforJavaScripthooksoruniqueelements,anduseclassesforstylingpurposes,especiallywhenapplyingsty


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
