Biến là một khía cạnh cơ bản của lập trình với JavaScript hoặc bất kỳ ngôn ngữ lập trình nào. Điều cần thiết là phải hiểu biến là gì và điều gì sẽ xảy ra khi sử dụng chúng. Trong thời gian dài nhất, JavaScript thường chỉ có một cách duy nhất để khai báo biến, với từ khóa
var myName = 'bob';function studentProfile[] {1. Kể từ khi ES6 [ES2015] xuất hiện, các từ khóa
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {2 và
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {3 đã có sẵn để khai báo biến. Ý tưởng đằng sau việc thêm các tính năng này vào ngôn ngữ là cung cấp các cách để kiểm soát cách các biến được khai báo, gán giá trị và hiển thị cho các phần khác của chương trình JavaScript thông qua khái niệm phạm vi.
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Biến Javascript
Bài viết này tập trung vào kiến thức cơ bản về biến — còn được gọi là Tuyên bố — trong JavaScript và các ví dụ về việc sử dụng chúng. Khái niệm cốt lõi của một biến là cung cấp một cách để khai báo và lưu trữ các giá trị có thể được sử dụng trong một chương trình. Khai báo một biến giống như đưa ra một giá trị cho một con trỏ có thể được sử dụng nhiều lần để tham chiếu giá trị được lưu trữ tại con trỏ đó. chức năng này
Các biến có thể được khai báo bằng cách sử dụng các từ khóa
var myName = 'bob';function studentProfile[] {1,
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {2 và
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {3var
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Khai báo biến
var myName = 'bob';function studentProfile[] {1 đã được sử dụng với javascript kể từ khi thành lập. Biến
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {1 [cũng là biến
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {2 và
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {3] được sử dụng để lưu trữ các loại dữ liệu khác nhau. Một biến có thể lưu trữ dữ liệu như chuỗi, số, booleans, đối tượng, hàm, v.v.
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {0
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Ví dụ trên cho thấy các loại dữ liệu khác nhau có thể được lưu trữ trong các biến. Tất cả các biến trên đều được khai báo với từ khóa
var myName = 'bob';function studentProfile[] {1. Khai báo một biến với
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {1 và bên ngoài bất kỳ khối mã nào khác sẽ đặt nó trong phạm vi toàn cầu, nghĩa là các biến có thể được truy cập từ bất kỳ phần nào khác của chương trình và có sẵn trên toàn cầu trong chương trình
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Các biến được khai báo bằng cách sử dụng
var myName = 'bob';function studentProfile[] {1 trong các khối mã hàm - được gọi là phạm vi hàm - chỉ có thể truy cập được trong hàm. Một ngoại lệ là khi một biến được gán giá trị nhưng không được khai báo bằng từ khóa
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {1, như minh họa cho
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {65 bên dưới. Gán một giá trị như thế này cho phép biến được truy cập bên ngoài hàm
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {6
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Các biến được khai báo bằng cách sử dụng
var myName = 'bob';function studentProfile[] {1 trong các khối mã logic - được gọi là phạm vi khối [ví dụ:.
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {67] - có thể truy cập bên ngoài khối
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {0let
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Khai báo biến
var myName = 'bob';function studentProfile[] {2 tương tự như
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {1 theo nhiều cách, nhưng bao gồm một số điểm khác biệt chính khi truy cập và phạm vi. Một biến được khai báo trong phạm vi toàn cầu bằng cách sử dụng
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {2 có thể được truy cập ở bất kỳ đâu trong chương trình. Hành vi này giống như những gì xảy ra với khai báo biến
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {1
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {5
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Ngoài ra, giống như với
var myName = 'bob';function studentProfile[] {1, các biến được khai báo với
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {2 trong phạm vi chức năng chỉ có thể truy cập được trong phạm vi chức năng và sẽ gây ra lỗi khi cố gắng truy cập bên ngoài phạm vi chức năng
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {8
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Tuy nhiên, khi sử dụng phạm vi khối, bất kỳ biến nào được khai báo bằng
var myName = 'bob';function studentProfile[] {2 bên trong khối đều không thể truy cập được bên ngoài khối và sẽ gây ra lỗi. Đây là điểm khác biệt chính đối với các biến được khai báo bằng từ khóa
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {1, có thể được truy cập bên ngoài khối
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {1const
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Khai báo biến
var myName = 'bob';function studentProfile[] {3 có nghĩa là được sử dụng cho dữ liệu sẽ không thay đổi - nói cách khác, dữ liệu sẽ không đổi. Cũng giống như với
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {1 và
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {2, các biến được khai báo bên ngoài hàm hoặc khối với
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {3 có sẵn cho phạm vi toàn cầu
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {6
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Và cũng giống như các biến được khai báo bằng
var myName = 'bob';function studentProfile[] {2, một biến được khai báo bằng
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {3 không thể truy cập được bên ngoài phạm vi chức năng hoặc phạm vi khối và sẽ gây ra lỗi
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {9
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Một lưu ý quan trọng về việc sử dụng các biến được khai báo với
var myName = 'bob';function studentProfile[] {3 là chúng không thể được gán lại. Điều này áp dụng cho biến
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {3 có dữ liệu bao gồm giá trị, mảng hoặc đối tượng
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {2
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Tuy nhiên, một điều quan trọng cần lưu ý là
var myName = 'bob';function studentProfile[] {3 cho phép thay đổi cả dữ liệu bên trong mảng và đối tượng.
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {0Kết luận
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
Các biến được sử dụng ở mọi nơi trong các chương trình JavaScript. Một vấn đề có thể xảy ra là có các biến trong các phạm vi khác nhau có cùng tên. Điều này có thể gây ra sự cố khi các giá trị được gán cho một biến bị ghi đè bởi các biến khác ngoài ý muốn. Đây là loại sự cố có thể không tạo ra thông báo lỗi nhưng vẫn dẫn đến sự cố. Tương tự như vậy, nhiều giá trị được gán được tạo mà không cần thay đổi chúng tại bất kỳ thời điểm nào trong chương trình. Tuy nhiên, có thể ghi đè giá trị của biến ngoài ý muốn, gây ra sự cố trong chương trình
Việc bổ sung
var myName = 'bob';function studentProfile[] {2 và
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope
var myName = 'bob';function studentProfile[] {3 nhằm giải quyết một số vấn đề này. Chúng đã trở thành công cụ hữu ích giúp nhà phát triển viết mã hạn chế khả năng hiển thị và sửa đổi dữ liệu - và do đó giúp nhà phát triển tránh các lỗi nhỏ hơn có thể gây ra sự cố lớn
console.log[`student name: ${myName}`];
}studentProfile[]; // student name: bob - variable 'myName' is part of the global scope