Introduction to JavaScript – Create a Variable: const

Introduction to JavaScript – Create a Variable: const

Let’s dive in and see a variable in the wild. Here is how you declare a constant variable:


const myName = 'Seth';
console.log(myName);
// Output: Seth

Let’s consider the example above:

1) const, short for constant, is a JavaScript keyword that creates a new variable with a value that cannot change.

2) myName is the variable’s name. Notice that the word has no spaces, and we capitalized the N. Capitalizing in this way is a standard convention in JavaScript called camelCasing because the capital letters look like the humps on a camel’s back.

3) = is the assignment operator. It assigns the value (‘Seth‘) to the variable (myName).

4) ‘Seth‘ is the value assigned (=) to the variable myName.
After the variable is declared, we can print ‘Seth‘ to the console with console.log(myName).

You can save any data type in a variable. For example, here we save numbers:


const myAge = 11;
console.log(myAge);
// Output: 11

In the example above, on line 1 the myAge variable is set to 11. Below that, console.log() is used to print 11 to the console.


Introduction to JavaScript – Create a Variable: const


Related Videos:

MiltonMarketing.com Related Videos.

Related Links:

JavaScript Glossary: on Variables

W3Schools.com on JavaScript Variables

Online JavaScript Compiler. Code on the go.

JavaScript Glossary

My little pony learning game in VB.NET

Hello World Android app built with Android Studio

Introduction to JavaScript – Control Flow: if/else Statements

Introduction to JavaScript – Properties

Introduction to JavaScript – Create a Variable: let

Introduction to JavaScript – Data Types

Who is this Android App Development course for?

Android Studios

Introduction to JavaScript – Review Types and Operators

Why Python is the cooler programming language

Python Glossary

Introduction to JavaScript – Variables: Review

Why do most sites use cookies?

FAQs

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.