Working With Strings0:34
double quotes, or backticks. So let's make a new variable called myString. We can use single quotes, like you've seen in the last video, we can say this is a string, and that should work fine. We can use double quotes as well. This is a string with double quotes, or we can use backticks. So let's replace the double quotes with backticks, and that should work as well. And just as an example of some operations you can do on strings, we can grab the length of the string, so count the number of characters using .length, and you can say this string is 35 characters long, or you can grab a certain character at a certain spot using the array.
Number Type Basics1:05
of the string, so count the number of characters using .length, and you can say this string is 35 characters long, or you can grab a certain character at a certain spot using the array syntax. So we can say myString, and give it an index, and I know we haven't covered arrays yet, but this is the syntax. And one should be the second character, so it should be an H. The next type is a number, so let's make a variable here named myNumber, and they can be whole numbers, so just give it a random number here. They can be decimal numbers, you can say 20.5 or whatever. They can be negative numbers, so if you put the negative sign in front, that should work.
They can be decimal numbers, you can say 20.5 or whatever. They can be negative numbers, so if you put the negative sign in front, that should work fine. Or there's even special numbers, like NaN, so you'll see this when you try to do an operation that doesn't make any sense. For example, if I try to multiply a number, so we have this variable, myNumber, with a string, that doesn't make sense, so the result should be NaN. myString, and we get NaN. We also have special numbers like positive infinity and negative infinity, so we can say Number.POSITIVE_INFINITY, and same for negative infinity.
Using BigInt Safely2:39
you can see that number there. And if you try to work with numbers greater than this number, then you'll get unexpected results. So for example, if we try to add 1, that looks correct. You can see we get 992 at the end. But if we try to add 2, you'll see that it's still 992. So we are getting unexpected results here. However, if we used a bigInt type, then it should work fine. So let's make a new variable here, let's say const, let's just name it x, and to define a bigInt, we can use bigInt as a method.
So let's make a new variable here, let's say const, let's just name it x, and to define a BigInt, we can use BigInt as a method. I know we haven't covered methods and functions yet, but this is how you would define it. And then within the brackets, you can give it the number you want to initialize it with. So let's say Number.Max_SAFE_INTEGER, and if we just log x here, you'll see that there's an n at the end of it, which signifies it's a BigInt type. So now if we wanted to do math operations on this, we can say x + 1, and this won't work because 1 is a number, and we can only do operations with other BigInts. So if we just add n to the end, that should work fine. So you can see we get that number plus 1, and if I do plus 2, that should work as well.
Booleans, Null, Undefined3:44
So if we just add n to the end, that should work fine. So you can see we get that number plus 1, and if I do plus 2, that should work as well. So yeah, make use of bigInts if you are working with large numbers. Next up we have booleans, and booleans represent a logical entity that can have two values, either true or false. So if we were to type true or false, that would obviously result in true or false, or we can store them in variables, like we've been doing. And you'll constantly be making use of booleans and boolean logic in conditionals, so your code will execute based on the value of the boolean. And again we'll do a dedicated video on booleans.
code will execute based on the value of the boolean. And again we'll do a dedicated video on booleans. The next two types are null and undefined, and they are related, and are two ways of representing nothing. So you already saw undefined when we created a variable, and did not give it a value, so say myVar. And you can see myVar is undefined because we did not give it a value. Now null on the other hand is when you explicitly tell something you want it to be nothing or empty or null. So if we say myVar equals null, then you can see myVar is now null.
Objects and Arrays5:08
So again, we had strings, numbers, bigInt, boolean, null and undefined, and symbols. And if you want, you can also make use of the typeof operator to check the type. So if you say typeof, we can make use of one of the variables we have here, say myString, you'll see it results in string, or myNumber should be number. Cool. Okay, let's move on to the reference types, and we'll start off with objects. So an object is a collection of related properties that make up that object. So for example, think of a person, what kind of properties does a single person have? Well, they have a name, an age, a job, and so on. So to make a person object, we can say const person, and we can use the curly brace syntax,
Well, they have a name, an age, a job, and so on. So to make a person object, we can say const person, and we can use the curly brace syntax, which is known as an object literal. So we'll open that up, and we'll give it some properties here. And these properties are referred to key value pairs. So we'll start with the name. We'll say Andre. And then a comma for the next one, say age, say 20, and job, web developer. Okay, and we can close that out, press enter. And now we have our person object.
Okay, and we can close that out, press enter. And now we have our person object. The values for the keys can be any type. As you can see, I just use strings and numbers here. But if you want, you can even use a nested object as a value. And again, we'll have a dedicated video on objects so we can dive deeper. Next up, let's take a look at arrays. So an array is a collection of multiple items. So say we had a group of people, we can represent that as an array of people. Now the types of each array item can be different.
So say we had a group of people, we can represent that as an array of people. Now the types of each array item can be different. But for this example, we'll just use an array of strings using the person's name. So let's make a new variable here, say const people. And we can use the square bracket syntax to define an array. And let's put a few strings in here that represent people. So Andre, Bob, and Lily. And this is our array of people. Again, you can have any types for each of the array items. In this case, we're just using strings here.
Again, you can have any types for each of the array items. In this case, we're just using strings here. But if you wanted to, you can even have an array of objects. So for example, we can have an object like we have up here for one of our items or for each item if you wanted to. And again, if we use the typeof operator, so if we do typeof person, it's an object. And if you do typeof people, it's an array, but arrays are objects as well. So reference types are also referred to as objects as well. So that's why it's returning object here.
Defining Functions7:28
So reference types are also referred to as objects as well. So that's why it's returning object here. Next up, we have functions. And a function is a fundamental part of any programming language and is a set of statements that performs a task or calculates a value. Again, we'll have a separate video on functions since they are such an important part of understanding programming languages. But for now, you can define a function along with a set of instructions. So it's a function, say, hi. So this would be the function declaration.
So it's a function, say, hi. So this would be the function declaration. And within the curly braces, we can give it a set of instructions. It can be more than one. But for this example, we'll just console.log, hi. And then we can close that out. So this would be the function declaration. But to call it, we can say, say, hi, with brackets. And this is the function call. And again, if we do typeof, say, hi, it returns function.
if you pass certain parameters to this function here. But we won't do that. And of course, as with most objects, you have some built-in functions you can make use of. So for example, if I wanted to get the certain date, so the number of the month, you can use getDate, or the number of the day in the month. And I believe there's one for getMonth as well. So this will be the current month. So as a review for reference types,
