null
Resum
El valor null
és un literal de JavaScript que representa un valor nul o "buit", per exemple, quan no s'ha asignat cap valor previ. És un dels valors primitius de JavaScript.
Sintaxi
null
Descripció
El valor null
és un literal (i no una propietat de l'objecte global, com ho és undefined
). A l'utilitzar les APIs, null
s'obté sovint en comptes de l'objecte esperat quan no hi ha cap objecte relevant. Quan es vol comprovar si un valor és null o be undefined és important recordar les diferències entre els operadors d'igualtat (==) i d'identitat (===) (en-US) (ja que amb el primer s'aplica una conversió de tipus).
// foo no existeix, no està definit i no s'ha inicialitzat mai
> foo
"ReferenceError: foo is not defined"
// Es sap que foo existeix ara però no té tipus ni cap valor assignat:
> var foo = null; foo
"null"
Diferència entre null
i undefined
typeof null // objecte (degut a un bug a ECMAScript, hauria de ser null)
typeof undefined // undefined
null === undefined // false
null == undefined // true
Especificacions
Especificació | Estat | Comentaris |
---|---|---|
ECMAScript 1a Edició. | Standard | Definició inicial. |
ECMAScript 5.1 (ECMA-262) The definition of 'null value' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'null value' in that specification. |
Standard |
Compatibilitat amb navegadors
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help! (en-US)
Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Suport bàsic | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Característica | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Suport bàsic | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |