二 js資料型別

2021-09-13 18:30:45 字數 2911 閱讀 4220

1.整數,小數,邏輯變數

console.log(1);//整數

console.log(1.0);//小數

console.log(true);//邏輯變數

2.定義變數

console.log(1);//整數

console.log(1.0);//小數

console.log(true);//邏輯變數

var a = 100;

console.log(a);

//undefined

var c = null; //沒有存放任何資料

console.log(c);

var d;

console.log(d);

var定義變數 ,執行時定義什麼型別就是什麼型別

沒有定義的變數或者沒有初始化的變數為 undefined

變數定義在**,變數的作用範圍在**.整個檔案

3.陣列

//定義陣列物件  根據下標索引順序存放

var arr = ;

console.log(arr);

arr = [1, 2, 3, 4];

console.log(arr);

//根據索引編號訪問資料

console.log(arr[2]);//訪問第3個元素

//陣列中可以存放任何不同型別元素

arr = [1, 2.2, 1.8, true, [0,1,2]]

console.log(arr[4]);

4.表

// {} 表,字典,  key --> value 類似map

var list_table = {};

list_table = ;

console.log(list_table);

//訪問已經存在的key value

console.log(list_table.hello);

console.log(list_table["0"]);

console.log(list_table["hello"]);

//訪問不存在的key

console.log(list_table.tmp_key); //undefined

//新增key value

list_table.tmp_key = [0,1,2];

console.log(list_table.tmp_key);

console.log(list_table);

//新增key value

list_table["lyc"] = 1992;

console.log(list_table);

5.函式物件

//函式物件的定義

//function關鍵字 (引數1, 引數2) {}函式體

//var call_func = function(param1, param2) {};

var call_func = function(param1, param2) ;

//函式呼叫

call_func(88, 888);

//函式的返回值

call_func = function(param1)

var ret = call_func(66666);

console.log(ret);

//函式名稱定義函式

function test_fun()

//呼叫

test_fun();

6.字串

//字串資料物件  "hello" 'hello' 單引號雙引號區別不大

var str = "lyc";

console.log(str);

str = 'lyc';

console.log(str);

7.拓展

//擴充套件

var bb = 3;

var cc = bb; //傳遞的是值,因為變數本身的記憶體就能存下基本資料型別

cc = 4;

console.log(bb); // bb = 3

//複雜資料物件,變數中存放的是引用,引用可以直接存放到我們的變數對應的記憶體中

bb = [1,2,3]; //把陣列的引用(快捷方式)傳給bb的記憶體

cc = bb;//傳遞的不是值而是引用,物件的位址,物件的快捷方式

cc[0] = 2; //[2,2,3]

console.log(bb); // bb = [2,2,3]

var system = ,

test_func : function() ,

age : 21,

*** : 1,

name : "lyc"

};system.test_name();

system.test_func();

console.log(system.name);

var cmd = [

function() ,

];cmd[0]();

function test_class(param1)

test_class();

//js,先定義好函式在執行 例子 重名函式

function testa()

testa();

function testa()

testa();

//結果 testa 2

var a_ptr = ;

var b_ptr = ;

a_ptr = b_ptr;

8.總結

整數,小數,邏輯變數     簡單資料型別  =傳的是值

字串物件,陣列物件,表,函式物件   = 傳遞的是引用

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...