The basic data types of JavaScript are: null (null value), undefined (undefined value), number (number), string (string), boolean (Boolean value), object (object).
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript defines 6 basic data types, as shown in the table:
Data type | Description |
---|---|
null | Null value means non-object |
undefined | Undefined value, indicating an unassigned initialization value |
number | Number, the value of mathematical operation |
string | String, representing information flow |
boolean | Boolean value, the value of logical operation |
object | Object, representing a data set with a composite structure |
You can use the typeof
operator to detect the basic structure of the data type.
Example 1
The following code uses the typeof operator to detect the types of common values.
console.log(typeof 1); //返回字符串"number" console.log(typeof "1"); //返回字符串"string" console.log(typeof true); //返回字符串"boolean" console.log(typeof {}); //返回字符串"object" console.log(typeof []); //返回字符串"object" console.log(typeof function(){}); //返回字符串"function" console.log(typeof null); //返回字符串"object" console.log(typeof undefined) ; //返回字符串"undefined"
Note:
The typeof operator returns one of the six basic types in the form of a string. However, through comparison, it can be found that there are two differences between the typeof return value and the above table. Brief explanation As follows:
Classify null as the Object type, rather than as a value of a special type (Null).
Classify function(,){} as Function type. That is, the function is regarded as an independent basic data type, rather than a special subclass of the Object type.
Example 2
Since the return type of null value is Object, using the following custom function can avoid the impact of null value on basic type detection.
//如果是 null 值,则先返回字符串 "null" 否则返回(typeof o)的值 function typeOf(o){ return (o === null) ? "null" : (typeof o); } console.log(typeOf(1)); //返回字符串"number" console.log(typeOf("1")); //返回字符串"string" console.log(typeOf(true)); //返回字符串 "boolean" console.log(typeOf({})); //返回字符串"object" console.log(typeOf(null)); //返回字符串"null" console.log(typeOf(undefined)); //返回字符串"undefined"
In JavaScript, a function is a special structure. It can be a collection of code, or a data type; it can be used as an object, or it can be used as a constructor to create a type. The usage of JavaScript functions is relatively flexible, which is also a manifestation of the agility of the JavaScript language (functional programming).
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What are the basic data types in javascript. For more information, please follow other related articles on the PHP Chinese website!

Classesarebetterforaccessibilityinwebdevelopment.1)Classescanbeappliedtomultipleelements,ensuringconsistentstylesandbehaviors,whichaidsuserswithdisabilities.2)TheyfacilitatetheuseofARIAattributesacrossgroupsofelements,enhancinguserexperience.3)Classe

Classselectorsarereusableformultipleelements,whileIDselectorsareuniqueandusedonceperpage.1)Classes,denotedbyaperiod(.),areidealforstylingmultipleelementslikebuttons.2)IDs,denotedbyahash(#),areperfectforuniqueelementslikeanavigationmenu.3)IDshavehighe

In CSS style, the class selector or ID selector should be selected according to the project requirements: 1) The class selector is suitable for reuse and is suitable for the same style of multiple elements; 2) The ID selector is suitable for unique elements and has higher priority, but should be used with caution to avoid maintenance difficulties.

HTML5hasseverallimitationsincludinglackofsupportforadvancedgraphics,basicformvalidation,cross-browsercompatibilityissues,performanceimpacts,andsecurityconcerns.1)Forcomplexgraphics,HTML5'scanvasisinsufficient,requiringlibrarieslikeWebGLorThree.js.2)I

Yes,onestylecanhavemoreprioritythananotherinCSSduetospecificityandthecascade.1)Specificityactsasascoringsystemwheremorespecificselectorshavehigherpriority.2)Thecascadedeterminesstyleapplicationorder,withlaterrulesoverridingearlieronesofequalspecifici

ThesignificantgoalsofHTML5aretoenhancemultimediasupport,ensurehumanreadability,maintainconsistencyacrossdevices,andensurebackwardcompatibility.1)HTML5improvesmultimediawithnativeelementslikeand.2)ItusessemanticelementsforbetterreadabilityandSEO.3)Its

React'slimitationsinclude:1)asteeplearningcurveduetoitsvastecosystem,2)SEOchallengeswithclient-siderendering,3)potentialperformanceissuesinlargeapplications,4)complexstatemanagementasappsgrow,and5)theneedtokeepupwithitsrapidevolution.Thesefactorsshou

Reactischallengingforbeginnersduetoitssteeplearningcurveandparadigmshifttocomponent-basedarchitecture.1)Startwithofficialdocumentationforasolidfoundation.2)UnderstandJSXandhowtoembedJavaScriptwithinit.3)Learntousefunctionalcomponentswithhooksforstate


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

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
