JavaScript Basics pt. 3 - Variable types, strings, and semicolons

JavaScript
Transcript

English (Auto-generated)

Welcome everyone the Javascript Basics Part three. Today we are going to cover Let Const and the different things you can do with strings. Let's go ahead and get started. So a few years ago, well last month when we went over how to declare variables using the Var method. But a few years ago ES six was introduced which was eight brand new, huge update to javascript. Now it brought with it a lot of new features, including replacements for bar when it comes to declaring variables. And one of those first replacements is known as left. So one of the upgrades that brings with it are making sure you can't use the same variable name twice which you could before with the bar. So let's check that out. Alright, so we're back over here and I replicate. I'll give you a second if you don't have that up. Hold up. You want to go ahead and pull that up. All right. So let's see here. So we have our string, let's make string out of town and say hello world again. Right now let's make another variable with the same name, strain using Bar. And let's have it say what's up. So now let's see what it prints out when I do console log for strength, which of these two is going to print out? It's going to print out the more recent ones because with far you can rename your shrinks. Let's see here. So we're gonna highlight bar a fun little one thing that's pretty at least popular you can use here and in the s code is if you highlight something and you want to select multiple instances of it. Once you highlight it, you'll see that it's kind of highlighted both. Um on a Mac it's command e on Windows Insurance control. D. But hit command D. Select both. You are. So I can replace both with let the Let keyword. And let's see what happens if we do that. It's an air because string has already been declared. You can't declare it twice. So that is pretty useful. So that way you don't accidentally end up declaring the same variable twice. If you've got a big file with lots of different variables in it. All right. Let's see here. Just get over it. There we are. So with let you can reassign a variables value though. Actually no embarrassing. Well type of that needs topography, you can re assign a variable value. So let's go ahead and look at that. So we've got let's use the same one. You can't re declare it. But what you can do is say I want it to say something different. So if you move the LET So now you're just calling on the variable itself not declaring a new variable. Let's say we wanted to say what's up and now it does that because you're not declaring a new variable, but you can change the value of that variable, which is neat. All right. Let's see here on deck. So let wasn't the only new method they added. They also have the constants. So, another new way to declare variables with essex is with the consequent word Unlike Let Const is read only meaning you can't reassign this variables value, trade that out. So, if we change let to const here we go. And we hit run. Yeah, assignment to constant value. You can't do it. Yes. Yeah. It's called conscious because it's a constant since its value can't be changed later. The common practice is to name your const variables with all uppercase letters and underscores separating the words instead of using a camel case. However, I do want to let you know, it's not something I actually fairly often. Uh and I guess my limited experience, but it's not something I've seen often. I've only seen it a few times where it's things you absolutely do not want change. All right. So, now we've got let we've got Const And we also started covering strings in the last lesson. But let's talk about more things you can do with strings. Job script because they are very very styling job script. So, what happens if you want to use a quotation mark in your strength? Let's go and try that. Yeah, let's go ahead. We'll just keep naming it string. Let's say let's just type out of something here. I said hello and they ignored me. Uh huh. Over all right. She's like that. That's what you're looking at me. And so what happens if we try and put that out nothing because we've got double quotes in there. You can't do that. So how can you get around that? So the first method you can do if you want is you can change type of quotation mark you're using. So you can swap between double quotes and single quotes josh. People recognize both types just fine. And it's a good and simple method to use if you want to use a quotation mark in your strength. So let's go ahead and try that out. So what happens if we replace these double quotes at the end with single quotes? Okay. The apostrophe but it's typically can be referred to as a single quote more after the job script. So look at that. That is now the same color as the rest of the string. Will try and run that and there you go. That works just fine. Yeah. However what happens if you want to use both types of quotation marks in your strength, let's try that out. Let's see what happens. Let's say then I said hello I am here. So now that's looking funky because you can't use both because you've already got a single quickly outside attaining your shrink two and you can't switch us double quotes because you've got double quotes on the inside here. So how are we going to get around that now? Well you can place a backslash in front of your double or single quote. So this group will know to ignore the acquisition mark and included as part of the strength. So let's try that out, it's not going to fix. So again that is going to be the backslash key in front of see now the backsplash and the apostrophe are different colors, but the rest of the strings the same college. So let's try that out. There you go. And that is how you're going to be able to use both types of strength or sorry, both types of quotation marks in your strengths. So the backsplash is known as the escape character. It's not only for quotation marks that can be used with other symbols too, including a backslash if you want to include a backslash in here. So let's just throw one sterling in there because we put a backlash that's going to be pink because it's expecting that it's going to escape something out of there. So you do it again. So there you go to use the double backslash and it gives you one pack. Yeah, sorry, I got the sniffles this morning. I got the heater on because it's a cold for southern California. Ak probably about Like 50° out and hitters make me a little stuffy. But so another thing you do with escape characters is if you want to make a new line of strike because you can't simply just hit enter because look what happens, it doesn't like that doesn't work with strengths. What what you can do is if you put back flash and indicates a new line, you want this to be a new line. Sure Syria. So that's how you can make that a new one. Get a sniffle again and I apologize. Yeah. All right. And then you can also use a similar method with an escape character to tap if you want to tap over because just like with spaces can't just tap over. For some reason javascript will ignore the spacing. And so what you can do, you can put here we go but backslash T for tab and there you are. That is how you will get a full tab in there. So these are some of the ways you can utilize escape characters in javascript. I just wanted to show off some of the more common ones you'll you may end up using with strength. Good stuff again and again. I apologize. So there's something called shrinking concatenation. So javascript when you use the plus operator with strings, it's called the concatenation operator. You can use this to Katnik strings together. And let's show off an example. So let's say let's get rid of that. We don't need a variable right now. Yeah. Yeah. And then let's combine two different streets. We've got one string there. Space plus signed other space let's say hello there world. So what prints out? It's going to say hello the world. It does. One thing you're gonna want to keep in mind though is that this plus sign will not count as a space. You'll have to add that either to the end of um your first string or the beginning of your second streaks. Now if we were in that again, there we go. We've got our space there. Yeah. So you can also use concatenation is shrinking coordination with variables. Let's try that out. Yeah, let's go back and add a variable. So now we declare a variable strength, Go ahead and can't be that this to our string we have and console log and there we go. That is how that works. You can also do that with multiple shrinks, you know, shrink again we've got can location there. So let's try that out. You know what's out of space? We want that space so you can catch me two different variables together. Yeah, there you go. So another thing you can do those things, there's a whole lot. I'm just showing you some some methods right here with strings and then we're going to learn about more operators and different tools in jobs from you can use with strings but they can also be used for other things to also those lessons for when we get into different types of data types. You can find the length of a shrink. So if you want to find out the length of a string, you can attach the dot length method to the end to find out how many characters are in it. So let's go ahead and try that out. Let's go ahead and empty that out and get her message, the console log. Let's just go ahead and get that rid of that altogether. Let's type out a message. Let's see. I loved Spider Man. No way. Because I did it was great. No spoilers. Don't spoil it for you, Don't worry. And that's a pretty long message. Let's say for some reason you want to find out how many characters are in that message. So we added dot like to the end we can't run. And that gives us 32. And that's going to include these spaces. So that's going to include every single character between those quotation marks. Yeah, Yeah. All right. So in this we went over, we learned about let me learn about cots. And another thing I will say is far. Yeah, because of Let and Const var is the older method. So you'll want to use Let and const now those are going to be much more common. So you want to use those. And then we also went over the different things you can do with strings including tree, Cat Nation escape characters. So you can use different types of quotation marks in your strings. And they're also we learned about how to find the length of the street. Another thing I do want to reiterate to you and this is going to be important is in javascript. It is best practice to add a semi colon to the end of your statement. So whenever whatever you have, like, let's declare a variable. Mhm let's just say and some semi colons, whenever, whenever you've got like a the, whenever you reach the end of a piece of code, the end of a statement, whatever is, it's typically best practice to add Nicola. Now you probably noticing that I have not been adding semi colons and the code runs time before the semi colons were used to physically tell job script once they ended. Um Now I'm not sure if this came in with es six or when this feature is added, but as you can probably tell because mike has been running fine without it, it's no longer needed, but you will see it a lot. Um and you may see employers inquire about it or you may see it, you may just see it mentioned. So it's kind of a newer practice to not use semicolons because again, there are necessary now, but a lot of people still do it because that's how they learned. Um and it doesn't um, it doesn't hurt your code in any way if you add some acorns, I just learned javascript without semi colons because our, the instructor had told us they were no longer needed. So I'm just used to typically doing it without them, but that's personally we're gonna be up to you, it may work best for you to add them um to kind of fit in with whatever what else is doing. But again, that's totally up to you. But for now that is it. And I will see you guys in the next lesson. We will get more into the different types of data types because as you can see there's a lot you can do with them. Next lesson we are going to get into numbers and mathematical operators. All right, okay, so have a great day.
294 Views 0 Likes 0 Comments

Let's get into new ways to declare variables, more things to do with strings, and semicolons!

Comment
Leave a comment (supports markdown format)