Primitive - MDN Web Docs Glossary: Definitions of ... - Mozilla

文章推薦指數: 80 %
投票人數:10人

In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods. There are 7 primitive ... SkiptomaincontentSkiptosearchSkiptoselectlanguageMDNWebDocsGlossary:DefinitionsofWeb-relatedtermsPrimitiveChangelanguageExamplePrimitivewrapperobjectsinJavaScriptSeealsoPrimitiveInJavaScript,aprimitive(primitivevalue,primitivedatatype)isdatathatisnotanobjectandhasnomethods.Thereare7 primitivedatatypes:string,number,bigint,boolean,undefined,symbol,and null. Mostofthetime,aprimitivevalueisrepresenteddirectlyatthelowestlevelofthelanguageimplementation. Allprimitivesareimmutable,i.e.,theycannotbealtered.Itisimportantnottoconfuseaprimitiveitselfwithavariableassignedaprimitivevalue.Thevariablemaybereassignedanewvalue,buttheexistingvaluecannotbechangedinthewaysthatobjects,arrays,andfunctionscanbealtered.ExampleThisexamplewillhelpyouunderstand thatprimitivevaluesareimmutable.JavaScript//Usingastringmethoddoesn'tmutatethestring varbar="baz"; console.log(bar);//baz bar.toUpperCase(); console.log(bar);//baz //Usinganarraymethodmutatesthearray varfoo=[]; console.log(foo);//[] foo.push("plugh"); console.log(foo);//["plugh"] //Assignmentgivestheprimitiveanew(notamutated)value bar=bar.toUpperCase();//BAZ Aprimitivecanbereplaced,butitcan'tbedirectlyaltered.PrimitivewrapperobjectsinJavaScriptExceptfornullandundefined,allprimitivevalueshaveobjectequivalentsthatwraparoundtheprimitivevalues: Stringforthestringprimitive. Numberforthenumberprimitive. BigIntforthebigintprimitive. Booleanforthebooleanprimitive. Symbolforthesymbolprimitive. Thewrapper'svalueOf()methodreturnstheprimitivevalue.Seealso JavaScriptdatatypes Primitivedatatype(Wikipedia) Glossary JavaScript string number bigint boolean null undefined symbol Foundaproblemwiththispage?EditonGitHubSourceonGitHubReportaproblemwiththiscontentonGitHubWanttofixtheproblemyourself?SeeourContributionguide.Lastmodified:Oct8,2021,byMDNcontributorsChangeyourlanguageSelectyourpreferredlanguageEnglish(US)DeutschEspañolFrançais日本語한국어Português(do Brasil)Русский中文(简体)Changelanguage



請為這篇文章評分?