Sitemap

JavaScript Declare VS Assign

Dec 22, 2021

--

Press enter or click to view image in full size

There’s a difference between declaring/creating a variable and assigning a value to it using the assignment operator.

Declare / Create a variable in JavaScript

So if we have something like this:

let myNum; // undefined

Here, we are creating/declaring a variable but it has no value in it. Thus it’s undefined. (undefined is a data type in JavaScript. we’ll cover in the next few videos)

Get Firdavsi Web Dev’s stories in your inbox

Join Medium for free to get updates from this writer.

Assign/ Set a value to the variable above

myNum = 20; // result or value is 20

Here we used = sign to set/assign the value 20 to variable called myNum

You can also declare and assign to an initial value at the same line. Like so:

// Create And Assign value at the same line
let myAge = 29;

Thanks for reading. Follow me for more JavaScript & Programming Tutorials.

--

--

Firdavsi Web Dev
Firdavsi Web Dev

Written by Firdavsi Web Dev

Hey there! I’m Firdavsi, a Math Lover. Obsessed with Computer Programming, Design and Science. Learn Web Development at https://w7school.com