Computer >> Máy Tính >  >> Lập trình >> Javascript

Sự khác biệt giữa kiểu dữ liệu nguyên thủy và không nguyên thủy trong JavaScript?

Các kiểu dữ liệu nguyên thủy là số, chuỗi, boolean, float, v.v. Các kiểu dữ liệu không nguyên thủy (Kiểu tham chiếu) là Mảng, Đối tượng, v.v.

Ví dụ

var number=10;
var stringValue="John";
var booleanValue=true;
var obj={};
var newArray=new Array();
console.log("The data type is="+typeof number);
console.log("The data type is="+typeof stringValue);
console.log("The data type is="+typeof booleanValue);
console.log("The data type is="+typeof obj);
console.log("The data type is="+typeof newArray);

Để chạy chương trình trên, bạn cần sử dụng lệnh sau -

node fileName.js.

Đầu ra

Ở đây, tên tệp của tôi là demo162.js. Điều này sẽ tạo ra kết quả sau -

PS C:\Users\Amit\JavaScript-code> node demo162.js
The data type is=number
The data type is=string
The data type is=boolean
The data type is=object
The data type is=object