Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the good practices for writing high-quality JS variables

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "what are the good practices for writing high-quality JS variables", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the good practices for writing high-quality JS variables?"

1. First choice const, then let

I declare variables using const or let, and the main difference between the two is that const variables require an initial value and cannot be reassigned once initialized.

On the other hand, the let declaration does not require an initial value, and we can reassign its value multiple times.

/ / const needs to initialize const pi = 3.14; / / cannot reassign const pi = 4.89; / / throws "TypeError: Assignment to constant variable"

On the other hand, the let declaration does not require an initial value, and we can reassign its value multiple times.

/ / let initialization is optional let result; / / let can be reassigned result = 14; result = result * 2

A good habit when choosing the declaration type of a variable is to prefer const, otherwise use let.

Function myBigFunction (param1, param2) {/ * lots of stuff... * / const result = otherFunction (param1); / * lots of stuff... * / return something;}

For example, if we are looking at the function body and see const result =... Declaration:

Function myBigFunction (param1, param2) {/ * lots of stuff... * / const result = otherFunction (param1); / * lots of stuff... * / return something;}

Not knowing what happens inside myBigFunction (), we can conclude that the result variable is assigned only once and is read-only after declaration.

In other cases, if you have to reassign variables multiple times during execution, you can use the let declaration.

two。 Minimize the range of variables

Variables exist in the scope they create. The code block and function body create a scope for the const and let variables. A good habit to improve the readability of variables is to keep them in a minimum scope.

For example, the following function is an implementation of the binary search algorithm:

Function binarySearch (array, search) {let middle; let middleItem; let left = 0; let right = array.length-1; while (left true binarySearch ([2,5,7,9], 1); / / = > false

The middle and middleItem variables are declared at the beginning of the function body. Therefore, these variables are available throughout the scope created by the body of the binarySearch () function.

The middle variable retains the intermediate index of the binary search, while the middleItem variable retains the intermediate index of the binary search.

However, the middle and middleItem variables are only used in the while loop code block. So why not declare these variables directly in the while code block?

Function binarySearch (array, search) {let left = 0; let right = array.length-1; while (left

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report