首頁 web前端 js教程 js中 關於undefined和null的區別介紹_基礎知識

js中 關於undefined和null的區別介紹_基礎知識

May 16, 2016 pm 05:36 PM
null undefined

當宣告一個變數未賦值一個變數沒有宣告的時候都是undefined型別的資料
js變數在使用前也必須先進行宣告
使用了一個未定義的變數x就會報錯。
如何判斷一個變數是否能用:

第一種方法:
if(typeof x=='undefined')
var  n=100;
n=new Date();
n= null;
null表示的是一個明確的已經知道的值,是一個空物件

第二種方法:

if(typeof(y)!='undefined' && v!=null)//也可以判斷一個變數是否可用

第三種方法:

var  v;

if(v)

      {//v會轉乘布林類型,然後在判斷。   

               alert('可使用');

           }

             else{   

                  alert('無法使用');

             }

我們很少用第一種和第二種方法,通常都用第三種。       

s中用undefined和null用==判斷就是相等的,用===判斷則是不相等的。

 

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Vue中的TypeError: Cannot read property '$XXX' of undefined,該如何處理? Vue中的TypeError: Cannot read property '$XXX' of undefined,該如何處理? Nov 25, 2023 pm 12:14 PM

Vue中的TypeError: Cannot read property '$XXX' of undefined,該如何處理?

PHP Notice: Undefined property: 的解決方法 PHP Notice: Undefined property: 的解決方法 Jun 22, 2023 pm 02:48 PM

PHP Notice: Undefined property: 的解決方法

undefined和null是什麼意思 undefined和null是什麼意思 Nov 20, 2023 pm 02:39 PM

undefined和null是什麼意思

undefined是什麼 undefined是什麼 Jul 31, 2023 pm 02:28 PM

undefined是什麼

c語言中null和NULL的差別是什麼 c語言中null和NULL的差別是什麼 Sep 22, 2023 am 11:48 AM

c語言中null和NULL的差別是什麼

如何解決 golang 中的 'undefined: path.Join” 錯誤? 如何解決 golang 中的 'undefined: path.Join” 錯誤? Jun 24, 2023 pm 01:12 PM

如何解決 golang 中的 'undefined: path.Join” 錯誤?

null和undefined有什麼不同 null和undefined有什麼不同 Nov 08, 2023 pm 04:43 PM

null和undefined有什麼不同

什麼時候用null和undefined 什麼時候用null和undefined Nov 13, 2023 pm 02:11 PM

什麼時候用null和undefined

See all articles