Biến được sử dụng để làm gì trong chương trình javascript?

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() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1. Kể từ khi ES6 (ES2015) xuất hiện, các từ khóa
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
2 và
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
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.

Javascript Variables

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() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1,
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
2 và
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
3

var

Khai báo biến

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1 đã được sử dụng với javascript kể từ khi thành lập. Biến
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1 (cũng là biến
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
2 và
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
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.

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
0

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() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1. Khai báo một biến với
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
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

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() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
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
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1, như minh họa cho
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
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

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
6

Các biến được khai báo bằng cách sử dụng

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1 trong các khối mã logic - được gọi là phạm vi khối (ví dụ:.
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
67) - có thể truy cập bên ngoài khối

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
0let

Khai báo biến

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
2 tương tự như
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
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
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
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
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
5

Ngoài ra, giống như với

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1, các biến được khai báo với
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
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

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
8

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() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
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
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1, có thể được truy cập bên ngoài khối

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1const

Khai báo biến

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
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
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1 và
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
2, các biến được khai báo bên ngoài hàm hoặc khối với
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
3 có sẵn cho phạm vi toàn cầu

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
6

Và cũng giống như các biến được khai báo bằng

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
2, một biến được khai báo bằng
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
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

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
9

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() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
3 là chúng không thể được gán lại. Điều này áp dụng cho biến
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
3 có dữ liệu bao gồm giá trị, mảng hoặc đối tượng

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
2

Tuy nhiên, một điều quan trọng cần lưu ý là

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
3 cho phép thay đổi cả dữ liệu bên trong mảng và đối tượng.

var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
0Kết luận

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() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
2 và
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
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