02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

const

we learn a new thing while talking about naming conventions. Say hello to constants.

Video Transcript

00:00So we have constants constants are like we said there's like things that never change and not only that they never change
00:07We want to make it easy for us and for anyone else that sees them to know whoa. Don't touch this
00:12Don't change it. This is never changing. It's something that will always be the same
00:17such as
00:18Pi such as the amount of people that are allowed to be in an elevator in a certain giving time
00:24That number never changes right? It's a constant. It's a number
00:28Let's say only 12 people are allowed to be on an elevator
00:30So to do that we we already seen how to create variables. We created a variable we give and we're gonna do this quickly
00:37But we're gonna obviously revisit this as we start building our code
00:41So we created a variable. Let's say we're gonna call this variable max users
00:45And max users is going to be the maximum amount of users that could be on an elevator and it's an integer and it's 12
00:51So far so good. Only problem with our max users is that our max users could be changed and
00:57Not only could it be changed. I think that I could change it. Maybe we don't want no one changing it ever
01:03We want it to be a number. We know what the number is and we don't know one touching it
01:06No matter what not us as well
01:09To create that all we have to do is instead of calling a var we could change that into saying that's called const
01:14Now const basically says this value is set once and no one will ever change it
01:20Now that's all we had to do to create a const now if I went to my max users
01:24users and set it to be a value of 20 and I ran my application is going to give me a warning telling me
01:31Line 8 which is the line that I'm in
01:34Illegal assignment to a variable specified as a constant
01:37Okay, so it's protecting me from changing a value or from other people
01:41Changing a value that we never want to change we got that we got that sorted
01:45So we know that we only want to put their values that never change which is tangible values a number
01:50An indicator a flag which is a yes or no like a Boolean which is true or false or a string
01:56We don't put a value put that put it once it's never gonna change. It's not inside of another object. It's right there
02:02Now the way we gonna the way we gonna the way we're going to recognize that element
02:07That's a constant is by putting it all in uppercase and whenever we have a space
02:11I'm just gonna put here a you know shift minus or whatever you call this the underscore
02:17Underline under whatever and then the name well max users that what that's what we had there
02:22And that's how we recognize that it's a constant now the way you interact with a constant is the exact same way only
02:28We're not allowed to add into it if but I could use it as something to add into things such as I could trace
02:34Alright, so we could use our max users constant the exact same way as we use it as we use any variable
02:39Only thing is we can't assign anything into it. We could still use it as a variable
02:44Such as it max users plus 10 if we run this then we'll just get that value back as long as we're not assigning that
02:51value back into the max users
02:53Constant then we're good as long as we don't just try to assign that value directly back into the max users as long as we don't
02:59Try to change it again after we said it then we're set one last note about this obviously. This is not allowed

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!