Hướng dẫn why are variables so important for javascript? - tại sao các biến lại quan trọng đối với javascript?

Các biến là một khía cạnh nền tảng của lập trình với JavaScript hoặc bất kỳ ngôn ngữ lập trình nào. Nó rất cần thiết để hiểu biến là gì và những gì mong đợi khi sử dụng chúng. Trong thời gian dài nhất, JavaScript thường chỉ có một cách để khai báo các 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ừ sự xuất hiện của ES6 (ES2015), 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, giá trị được gán 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.

Biến JavaScript

Bài viết này tập trung vào những điều cơ bản của các biến - còn được gọi là khai báo - trong JavaScript và các ví dụ về việc sử dụng của 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. Tuyên bố một biến giống như đưa ra một giá trị một con trỏ có thể được sử dụng nhiều lần để tham chiếu giá trị được lưu trữ ở 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 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

Tuyên bố 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à
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
3Variables) đượ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, chức năng, v.v.

var myName = 'bob';
var myAge = 34;
var clubMember = true;
var schoolSupplies = ['pencil', 'notebook', 'pen', 'eraser'];
var schoolSchedule = {
math: 'Advanced Algebra',
science: 'Physics',
literature: 'Modern Chinese Literature',
};
var sayHello = function () {
console.log('hello');
};

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 đượ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 đặt nó trong phạm vi toàn cầu, có 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.global scope, meaning the variables can be accessed from any other part of the program and is available globally within the program.

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ã chức năng - được gọi là phạm vi chức năng - chỉ có thể truy cập trong hàm. Một ngoại lệ là khi một biến được gán một 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ư được hiển thị cho
function getStudentGrade() {
testResults = [75, 86, 93, 87];
var testResultsAverage =
testResults.reduce((a, b) => a + b) / testResults.length;
return testResultsAverage;
}
console.log(testResults); // 85.25 - variable 'testResults' is part of the global scope
5 bên dưới. Việc 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.function scope - are only accessible within the function. An exception is when a variable is assigned a value but not declared using the
var myName = 'bob';function studentProfile() {
console.log(`student name: ${myName}`);
}
studentProfile(); // student name: bob - variable 'myName' is part of the global scope
1 keyword, as shown for
function getStudentGrade() {
testResults = [75, 86, 93, 87];
var testResultsAverage =
testResults.reduce((a, b) => a + b) / testResults.length;
return testResultsAverage;
}
console.log(testResults); // 85.25 - variable 'testResults' is part of the global scope
5 below. Assigning a value like this allows the variable to be accessed outside of the function.

function getStudentGrade() {
testResults = [75, 86, 93, 87];
var testResultsAverage =
testResults.reduce((a, b) => a + b) / testResults.length;
return testResultsAverage;
}
console.log(testResults); // 85.25 - variable 'testResults' 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ã logic - được gọi là phạm vi khối (ví dụ
function getStudentGrade() {
testResults = [75, 86, 93, 87];
var testResultsAverage =
testResults.reduce((a, b) => a + b) / testResults.length;
return testResultsAverage;
}
console.log(testResults); // 85.25 - variable 'testResults' is part of the global scope
7) - có thể truy cập bên ngoài khối.block scope (ex.
function getStudentGrade() {
testResults = [75, 86, 93, 87];
var testResultsAverage =
testResults.reduce((a, b) => a + b) / testResults.length;
return testResultsAverage;
}
console.log(testResults); // 85.25 - variable 'testResults' is part of the global scope
7) - are accessible outside of the block.

if (true) {
var newName = 'ned';
}
console.log(newName); // ned - variable 'newName' is part of the global scope

let

Tuyên bố 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ố khác biệt chính khi nói đến 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 cứ đâ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.

let studentName = 'melvin';function greetStudent() {
console.log(`Greetings, ${studentName}!`);
}

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 hàm chỉ có thể truy cập trong phạm vi hàm và sẽ gây ra lỗi khi cố gắng truy cập bên ngoài phạm vi hàm.

function chooseClub() {
let clubSelection = 'chess club';
console.log(clubSelection);
}
console.log(clubSelection); // ERROR - 'clubSelection' is only accessible within the function scope

Tuy nhiên, khi sử dụng phạm vi khối, bất kỳ biến nào đượ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 bên trong một khối không thể truy cập được bên ngoài khối và sẽ gây ra lỗi. Đây là một sự khác biệt chính cho các biến được khai báo với 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, có thể được truy cập bên ngoài một khối.

if (10 > 5) {
let answer = 'Ten is greater than five';
}
console.log(answer); // ERROR - 'answer' is only accessible within the block scope

const

Tuyên bố 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. 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.

const friendName = 'Kalvin';function sayFriendName() {
console.log(`Hi, ${friendName}!`);
}
sayFriendName(); // Hi, Kalvin! - variable 'friendName' is part of the global scope

Và giống như 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, một 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 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.

function getLunch() {
const specialMeal = 'pasta';
}
console.log(specialMeal); // ERROR - 'specialMeal' is only accessible within the function scopeif ('pizza' === 'pizza') {
const favoriteFood = 'cheese pizza';
}
console.log(favoriteFood); // ERROR - 'favoriteFood' is only accessible within the block 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() {
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 chỉ định lại. Điều này áp dụng cho các 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 các giá trị, mảng hoặc đối tượng.

// VALUE
const courseLength = '2 hours';
courseLength = 120; // ERROR - 'courseLength' cannot be reassigned// ARRAY
const courseTitles = ['Intro to Math', 'English Literature', 'Biology'];
courseTitles = ['Physical Education', 'Chemistry', 'Spanish I']; // ERROR = 'courseTitles' cannot be reassigned// OBJECT
const courseProfile = {
name: 'Chemistry',
days: ['Monday', 'Wednesday'],
time: '9:30am',
instructor: 'Gil',
};
courseProfile = {
name: 'German Literature',
days: ['Friday'],
time: '14:00',
instructor: 'Bob',
}; // ERROR - 'courseProfile' cannot be reassigned

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 cả hai dữ liệu bên trong các mảng và đối tượng được thay đổi.

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

Sự kế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à từ việc 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ể tạo ra các vấn đề khi các giá trị được gán cho một biến bị ghi đè bởi các biến khác một cách vô tình. Đây là một loại vấn đề có thể không tạo ra thông báo lỗi, nhưng vẫn sẽ dẫn đến các vấn đề. Tương tự như vậy, nhiều giá trị được gán được tạo ra mà không cần thay đổi chúng tại bất kỳ điểm nào trong chương trình. Tuy nhiên, nó có thể ghi đè lên giá trị của biến vô ý, gây ra vấn đề 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 có nghĩa là để giải quyết một số vấn đề này. Họ đã trở thành các công cụ hữu ích để giúp các 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 các nhà phát triển tránh các lỗi nhỏ hơn có thể gây ra vấn đề lớn.

Mục đích của các biến là gì?

Các biến được sử dụng để lưu trữ thông tin để được tham chiếu và thao tác trong một chương trình máy tính. Họ cũng cung cấp một cách ghi nhãn dữ liệu với một tên mô tả, vì vậy các chương trình của chúng tôi có thể được người đọc và chính chúng ta hiểu rõ hơn. Thật hữu ích khi nghĩ về các biến như các container chứa thông tin.to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information.

JavaScript và các biến của nó là gì?

Một biến JavaScript chỉ đơn giản là một tên của vị trí lưu trữ.Có hai loại biến trong JavaScript: biến cục bộ và biến toàn cầu.Có một số quy tắc trong khi tuyên bố một biến JavaScript (còn được gọi là định danh).Tên phải bắt đầu bằng một chữ cái (A đến Z hoặc A đến Z), dấu gạch dưới (_) hoặc Dollar ($).a name of storage location. There are two types of variables in JavaScript : local variable and global variable. There are some rules while declaring a JavaScript variable (also known as identifiers). Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.

Có cần thiết để viết VAR trong JavaScript không?

Luôn luôn khai báo các biến JavaScript với var, let hoặc const.Từ khóa VAR được sử dụng trong tất cả các mã JavaScript từ năm 1995 đến 2015. Từ khóa LET và const đã được thêm vào JavaScript vào năm 2015. Nếu bạn muốn mã của mình chạy trong các trình duyệt cũ hơn, bạn phải sử dụng VAR. . The var keyword is used in all JavaScript code from 1995 to 2015. The let and const keywords were added to JavaScript in 2015. If you want your code to run in older browsers, you must use var .