SoFunction
Updated on 2025-03-03

JavaScript declares the four brothers of variables (var, let, function, const)

Four brothers background

In a nameprogramming languageThere are villagers with different surnames in the village. For exampleJavaPythonGolangJavaScriptWait for surnames, and what I want to introduce next isJavaScriptSome stories about this surname.

Let’s first introduce the four brothers in this surname:Brother varfunction brother、 Let brother、 Const. What these four brothers can do is a bit similar. I wonder if it was the village chief's intentional arrangement. However, it is gratifying that the village chief was considerate when making arrangements, and they also had their own characteristics (spleen) and (qi). All are pretty good to pretend! Each of them has a backpack with a password lock, which can help you carry something. Overall, they are very capable. After all, I just said that it is quite capable~

Characteristics of the four brothers

Let’s take a look at their respective characteristics through a table:

Feature Characters English name Chinese name Features
var Var Javascript Chicken baby It’s easy to show off. After placing things, you have to tell the landlord the password: window, and after locking, you can open and change the contents. I don’t know what the lock can do.
function Function Javascript Chicken method The things you install are very unusual, one pair () and one pair {}, and you can only install this kind of thing. This thing is like magic, and can do something well. A small piece of material: As long as the chickens and babies show off in the field of chicken methods, they can be arranged clearly, and they can be specially designed to treat the chickens and babies.
let Let Javascript Chicken pen The chicken pen is younger than the first two. He doesn't like to show off. And when you install something, it is like a fence, which only takes effect in the fence. Many JavaScript programmers like him very much.
const Const Javascript Chicken pit He Chicken Pin was born in 2015 and also has the effect of a fence. This guy is like throwing a stone into a pit. After throwing it, he can't throw anything else. If he throws it, he will lose his temper.

Four Brothers Story

  -> undefined
   -> undefined
 -> undefined
 -> undefined
windowexplain 'What are you looking for here?  I don't have what you want, get out of here'
var writeWhen = '2022-03-20 22:50:30';  -> '2022-03-20 22:50:30'
//【Voiceover】 The chicken baby put on a content marked as writeWhen, but he made a mistake again, letting the landlord know
function writeHow() {
  // Where to use magic}
//【Voiceover】The chicken method has a magic package
let writeWhen = '2022-03-20' -> SyntaxError: writeWhen has been declared
鸡栏explain:'what? Who has installed this writeWhen?  It seems I have to change one'

const writeHow = 'function' -> SyntaxError: writeHow has been declared
鸡坑explain:'Ouch!  Has anyone pretended this?  Brother Lan, let’s not install other brothers or what we have installed.

鸡栏explain:'Okay, chicken baby and chicken method, don't pretend to be me and the chicken pits'

Chicken baby、鸡法explain: 'Understand we don't want to see SynctaxError, keep working'
//[Voiceover] They reached an agreement that as long as they were loaded by chicken pens and chicken pits, the four brothers could not be declared again, and the process was irreversible.
let writeWho = 'In short' -> 'In short'
//【Voiceover】The fence of the chicken bar has been built
const writeWhere = 'juejin' -> 'juejin'
//【Voiceover】 The chicken baby put on a content marked as writeWhen, but he made a mistake again, letting the landlord know
 -> '2022-03-20 22:50:30'
   -> undefined
 -> undefined
 -> undefined
windowexplain 'Hehe, the chicken baby likes to show off. I know his situation, and I can't do anything about the other three brothers'

function writeHow () {
    (curTime, writeWho);
    var curTime = '2022-03-22 23:10';
    const writeWho = 'name'; 
    (writeWho);
    return 'write with story';
}
//【Voiceover】 We redefined writeHow and customized magic
writeHow(); -> log[undefined, 'In short'] log('name') 'write with story'
//【Voiceover】 This writeHow defined by the chicken method uses a magic, prints 2 lines of results and has a return value.// This is the uniqueness of the chicken methodFirst line: PrintcurTimeandwriteWho,Chicken baby声明的命名空间会exist预检阶段have变量提升过程,
Even if the value is not assigned at this time,You can also access variables,No exception is thrown。writeWhoDue to the existence of scoped chains
existwriteHow所exist的区域内找到了writeWho,Returned results。所以Print了[undefined, 'In short']

Line 2 and 3:Defined in different wayscurTimeandwriteWhoVariable and assign value

Line 4:PrintwriteWho,The third line has been redefinedwriteWho,becauseconsthave“Fence”Features,So at this time
writeWhoBecomewriteHow的私have变量,No longer related to the variable of the same name in the outside world。所以Print了'name'

Line 5: The return value of the function is'write with story'

Conclusion

  • All are ways to define variables.functionOnly functions can be defined, andletconstandvarFunctions and normal variables can be defined
  • quiltconstDefined variablesCan't assign directly againconstUsed to define onlyconstant, the value will not change
  • varThe variables defined globally are mapped towindowDown
  • varandfunctionCanThe same variable that has been defined repeatedly defines the assignment, but as long asletandconstAfter declaration, it cannotDefine again

All of the above are non-strict models

This is the article about these four brothers (var, let, function, const) who declare variables in JavaScript ends. For more related contents of JavaScript declaration variables, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!