Introduction to JavaScript – Create a Variable: let

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

const myName = 'Seth';

myName = 'Hannah';

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.)

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

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?