js 資料型別

2021-09-25 07:22:56 字數 1682 閱讀 7401

number(數值)、boolean(布林:判斷真假)、string(字串)、undefined(宣告未賦值)、null(宣告賦值為空,型別無法確定,在接下來的使用中進行確定)、symbol(唯一標誌符)

function(函式)、array(陣列)、object(物件)

兩種資料型別變數的儲存方式的不同,在變數賦值發生變化時出現的不同情況

1、原始資料型別的變數值儲存在棧中:

var a = 23;   var i = a;   a = 34;   此時,i 中的值仍為23;(a 只是將變數值賦值給了 i,之後 a 的改變與 i 無關);

2、引用資料型別的變數在棧中儲存了變數值的儲存位址,實際變數值則儲存在堆中(假設b的變數值的儲存位址為0x001)

var b = [ 23,34,45,3 ];  var j = b;  b = [ 90,67,5,3 ];   此時b 中的值為 [ 90,67,5,3 ];(b 則是將變數值的儲存位址賦給了 j,此時b 和 j 的變數值儲存位址同時指向位址#001);

如圖所示:

js六大假值:null、undefined、false、0、nan、空字串''  ==>  返回均為false;

typeof判斷字串型別,但無法識別null、array、object,此三者輸出型別均為object

數字, typeof( 1 )  == 'number' ;

字串,typeof( 'jia' ) == 'string';

布林,typeof( true ) == 『boolean』;

null,typeof( null ) == 'object';

undefined,typeof( undefined ) == 'undefined';

symbol,typeof( symbol() ) == 『symbol』;

array,typeof( ) == 『object』;

function,typeof( function(){} ) == 'function';

object,typeof( {} ) == 『object』;

null  和  undefined 的比較:

null == undefined; // true

null == undefined; // false

unicode編碼

數字範圍:0 ~ 9

漢字範圍:\ue400 ~ \u9fa5

字母範圍:a ~ z (字母之間存在其他字元的unicode碼)

其他字元

typeof判斷型別

var n = '0' ;

var result = ( typeof n == 'number' || n != ' ') ? '數字' : ((n >= '\u4e00' && n <= '\u9fa5') ? '漢字' : ((n >= 'a' && n <= 'z'|| n >= 'a' && n <= 'z') ? '字母' : '其他字元')) ;

console.log('result ==> ', result);

js資料型別

一.原始資料型別 1.typeof 運算子。var a alert typeof a 輸出結果為 undefined 2.undefined 未定義 型別 當變數未定義時,該變數值被預設為undefined 如 var a alert typeof a 輸出結果為 undefined 注意值unde...

js資料型別

js 基本資料型別 undefined null boolean number string js 操作符 typeof檢測給定變數的資料型別。返回值表示的型別 undefined 值未定義。boolean 值為布林型別。string 值為字串。number 值為數字。object 值為物件或nul...

js資料型別

var num 1,boo true,aa null,bb,str mary arr 1,2,4,8 obj arrnew new array 1,2,3 strnew new string 1,2,3 用 typeof 檢測變數的型別 console.log number typeof num n...