Introduction to JavaScript – Create a Variable: let

When you attempt to assign a value to a const variable like this example shows:

[javascript]

const myName = ‘Seth’;

myName = ‘Hannah’;

[/javascript]

If you attempt the code above (assigning a value to a const variable) you will receive the following error:

TypeError: Assignment to constant variable.
JavaScript threw an error because you assigned a new value to a constant variable. Constant variables, as their name implies, are constant — you cannot assign them a different value.

Let variables, however, can be reassigned: (see code example below.)

[javascript]

let meal = ‘Enchiladas’;
console.log(meal);
meal = ‘Tacos’;
console.log(meal);
// output: Enchiladas
// output: Tacos

[/javascript]

In the example above, the let keyword is used to create the meal variable with the string ‘Enchiladas‘ saved to it. On code line three (above), the meal variable is changed to store the string ‘Tacos‘.

You may be wondering, when to use const vs let. In general, only use const if the value saved to a variable does not change in your program.


Introduction to JavaScript – Create a Variable: let


Related Videos:

MiltonMarketing.com Related Videos.

Related Links:

My little pony learning game in VB.NET

JavaScript Glossary

See the JavaScript Glossary on Variables

W3Schools.com JavaScript Variables

Online JavaScript Compiler. Code on the go.

CSS, HTML, JAVASCRIPT, Online Compiler. Code on the go by replit.it

How to make a go-back button with PHP code?

Hello World Android app built with Android Studio

Introduction to JavaScript – Data Types

Introduction to JavaScript – Review Types and Operators

Introduction to JavaScript – Variables: String Interpolation

Why Python is the cooler programming language

Python Glossary

Introduction to JavaScript – Variables: Undefined

Introduction to JavaScript – Properties

Introduction to JavaScript – Control Flow: if/else Statements

Who is this Android App Development course for?

Introduction to JavaScript – Variables: Review

Android Studios

Why do most sites use cookies?

Connected through code, Choose Your Platform!

About the Author: Bernard Aybout

In the land of bytes and bits, a father of three sits,With a heart for tech and coding kits, in IT he never quits.At Magna's door, he took his stance, in Canada's wide expanse,At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance.With a passion deep for teaching code, to the young minds he showed,The path where digital seeds are sowed, in critical thinking mode.But alas, not all was bright and fair, at Magna's lair, oh despair,Harassment, intimidation, a chilling air, made the workplace hard to bear.Management's maze and morale's dip, made our hero's spirit flip,In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene,Yet in tech, his interest keen, continues to inspire and convene.