Qhov txawv ntawm null thiab undefined hauv JavaScript

Cov txheej txheem:

Qhov txawv ntawm null thiab undefined hauv JavaScript
Qhov txawv ntawm null thiab undefined hauv JavaScript

Video: Qhov txawv ntawm null thiab undefined hauv JavaScript

Video: Qhov txawv ntawm null thiab undefined hauv JavaScript
Video: Data Science with Python! Filtering Data with pandas 2024, Kaum ib hlis
Anonim

Qhov Tseem Ceeb Qhov txawv - null vs undefined hauv JavaScript

JavaScript yog siv los ua tus neeg siv khoom-sab lus sau los ua kom cov nplooj ntawv web dynamic. Nws yooj yim siv nrog HyperText Markup Language (HTML). JavaScript yog qhov muaj txiaj ntsig los ua kom muaj kev sib cuam tshuam thiab tsim kom muaj kev sib txuas ntxiv. Thaum programming nrog JavaScript, nws yuav tsum tau khaws cov ntaub ntawv. Qhov sib txawv yog qhov chaw cia khoom uas qhov kev zov me nyuam tuaj yeem tswj tau. Hloov pauv yog qhov chaw nco. Txhua qhov sib txawv muaj ib hom. Nws nyob ntawm qhov ntau ntawm qhov tseem ceeb uas tuaj yeem khaws cia hauv qhov chaw nco ntawd. JavaScript txhawb ntau hom ntaub ntawv. Cov ntaub ntawv tseem ceeb yog Cov Naj Npawb, Txoj hlua thiab Booleans. Cov lej khaws cov lej muaj nuj nqis, Cov hlua khaws cov cim ua ntu zus thiab Booleans khaws qhov tseeb lossis cuav. JavaScript kuj muaj cov ntaub ntawv sib xyaw uas yog cov khoom thiab lwm yam. Muaj ob qhov sib txawv. Lawv yog null thiab undefined. Kab lus no tham txog qhov sib txawv ntawm null thiab undefined hauv JavaScript. Qhov sib txawv tseem ceeb ntawm null thiab undefined hauv JavaScript yog qhov null yog siv los muab qhov tsis muaj nuj nqis rau qhov sib txawv thaum undefined yog siv thaum qhov sib txawv tau tshaj tawm tab sis tsis muab nrog tus nqi.

Dab tsi yog null hauv Javascript?

JavaScript txhawb nqa Object Oriented Programming. Ib qho program lossis software tuaj yeem ua qauv rau hauv cov khoom siv JavaScript. Cov khoom no sib txuas lus nrog lwm yam khoom. JavaScript cov khoom yog sau siv curly braces. Cov khoom vaj khoom tsev yog sau ua lub npe, tus nqi khub. Lawv raug cais los ntawm ib qho comma. e.g. var tub kawm={npe: “Ann”, qhab nia: 65};

Qhov txawv ntawm null thiab undefined hauv JavaScript
Qhov txawv ntawm null thiab undefined hauv JavaScript

Thaum tus programmer xav muab qhov tsis muaj nqi rau qhov sib txawv, nws tuaj yeem siv cov ntaub ntawv hom null. Hom ntaub ntawv no suav hais tias yog ib yam khoom.

Saib cov lus hauv qab no JavaScript.

var x=null;

document.write(x);

document.write(typeof(x));

The document.write(x) yuav muab tus nqi x. Nws yog null. Hom x yog khoom.

Saib cov piv txwv hauv qab no.

var tub kawm={npe: “Ann”, qhab nia: 65};

student=null;

Raws li cov lus hais saum toj no, tus nqi ntawm tus menyuam kawm ntawv yog null. Hom ntaub ntawv yog khoom.

Dab tsi yog qhov tsis tau txhais hauv Javascript?

Hauv JavaScript, thaum qhov sib txawv tau tshaj tawm tab sis tsis muab tus nqi, ces nws tsis tau txhais.

Saib cov lus hauv qab no JavaScript. Yog muaj nqe lus xws li var x; qhov twg x yog qhov sib txawv. Ces x muaj tus nqi undefined. Hom ntaub ntawv tseem tsis tau txhais.

var x;

document. write(x);

document.write(type(x));

Qhov no yuav qhia tus nqi ntawm nplooj ntawv HTML. Nws muab undefined. Yog li ntawd, nws muaj ib tug nqi ntawm undefined. Thaum sau document.write(type(x)); thiab reloading nplooj ntawv, nws tseem yuav muab undefined. Yog li, qhov sib txawv x muaj tus nqi uas tsis tau txhais thiab hom tseem tsis tau txhais.

Saib nqe lus hauv qab no thiab.

var tub kawm;

document.write(student);

Tus menyuam kawm ntawv sib txawv muaj tus nqi tsis tau txhais. Hom ntawm qhov sib txawv ntawd kuj tsis tau txhais.

Nws tseem tuaj yeem teeb tsa tus nqi sib txawv rau undefined. Saib cov lus hauv qab no.

var tub kawm=undefined;

document.write(student);

document.write(typeof(student));

Tam sim no tus menyuam kawm ntawv hloov pauv tau muaj tus nqi tsis tau txhais. Hom ntawm cov menyuam kawm ntawv sib txawv kuj tseem tsis tau txhais.

Dab tsi yog qhov zoo sib xws ntawm null thiab undefined?

Ob leeg yog hom ntaub ntawv hauv JavaScript

Qhov txawv ntawm null thiab undefined yog dab tsi?

Null vs Undefined

Tus null yog hom ntaub ntawv hauv JavaScript siv los muab qhov tsis muaj nqi rau qhov sib txawv. Qhov tsis tau txhais yog hom ntaub ntawv hauv JavaScript siv thaum qhov sib txawv tau tshaj tawm tab sis tsis muab nrog tus nqi.
nqi
Thaum qhov sib txawv yog muab rau null, tus nqi yog null. Thaum qhov sib txawv yog muab tsis tau txhais, tus nqi yog undefined.
Data Type
Thaum qhov sib txawv yog muab null, hom ntaub ntawv yog ib qho khoom. Thaum qhov sib txawv yog muab tsis tau txhais, cov ntaub ntawv hom yog suav tias yog undefined.

Summary – null vs undefined in JavaScript

JavaScript yog cov lus siv sab saud siv rau kev txhim kho lub vev xaib. Nws coj kev sib cuam tshuam rau lub vev xaib. Nws kuj yog siv los tsim cov kev pab cuam online, video games. Nrog rau HTML thiab CSS, nws yog ib qho tseem ceeb thev naus laus zis rau World Wide Web. Ntau browsers txhawb JavaScript. Thaum sau cov kev pab cuam hauv JavaScript, nws yuav tsum tau khaws cov ntaub ntawv. Cov ntaub ntawv khaws cia rau hauv qhov sib txawv. Txhua qhov sib txawv muaj cov ntaub ntawv hom. Ob tug ntawm lawv yog null thiab undefined. Qhov txawv ntawm null thiab undefined hauv JavaScript yog qhov null yog siv los muab qhov tsis muaj nuj nqis rau qhov sib txawv thaum tsis tau txhais yog siv thaum qhov sib txawv tau tshaj tawm tab sis tsis muab nrog tus nqi.

Download the PDF of null vs undefined in JavaScript

Koj tuaj yeem rub tawm PDF version ntawm kab lus no thiab siv nws rau offline hom phiaj raws li daim ntawv ceeb toom. Thov rub tawm PDF version ntawm no: Qhov txawv ntawm null thiab undefined hauv JavaScript

Pom zoo: