در حال بارگذاری ...

Dynamic Typing Overview0:00

Like php, JavaScript is a dynamically-typed language, basically meaning that we can assign any kind of value to any kind of variable without any problems whatsoever. But php is what we would call a stronger typed language than JavaScript, because in php we can use things like type hints to basically say that certain parameters are of certain types, and functions return certain kinds of values. We don't have that in JavaScript. And you might think, well, how do we know what kind of value a parameter is or a variable? And as far as an explicit code literal thing, we don't. We do have something that we can use to determine what kind of value it is. Then we will talk about that later. But it's something that we have to execute. It's not something that we can see physically inside of the code.

Understanding Undefined0:51

But it's something that we have to execute. It's not something that we can see physically inside of the code. So let's just jump right in. And we are going to start with probably the most basic type of value, and it's called undefined. Whenever you declare a variable but you don't assign a value to it, it is undefined. The same is true for a parameter in a function. If you call a function that has a parameter but you don't pass an argument to that function, that parameter is also undefined. This can be a little confusing whenever we talk about null here in a few minutes. But for right now, just know that we have this thing called undefined. And it is for variables or parameters that don't have a value.

Working with Strings1:26

But for right now, just know that we have this thing called undefined. And it is for variables or parameters that don't have a value. Next, we have string. And we have two kinds of strings. We have the double-quoted string, which of course uses double quotes. Then we have the single-quoted string, which uses single quotes. And as I mentioned in the previous episode, there is no difference between using double quotes or single quotes. They create a string, and that's it. But it is advantageous because this gives us the ability to use an apostrophe inside of a string without having to escape it. So all you would have to do is use that apostrophe inside of a string that is denoted with double quotes.

But it is advantageous because this gives us the ability to use an apostrophe inside of a string without having to escape it. So all you would have to do is use that apostrophe inside of a string that is denoted with double quotes. The same is also true for using double quotes inside of a single-quote string. Because our string is created with a single quote, we don't have to escape those double quotes. However, if we do need to use a double quote inside of a double-quoted string, we have to escape that. The same is true for single quotes. So there is that. So we have strings, obviously. Next, we have number. Now, php has two types of numbers.

Numbers and Booleans2:39

Next, we have number. Now, php has two types of numbers. It has integers, and then it has floating points. Now, we have those values, but as far as JavaScript is concerned, they are the same type. And it's just called number. So we have two numbers. We have the whole number, the integer. Then we have the decimal number. And all decimal numbers in JavaScript are floating points. Then we have Boolean, which we have a true value, which is the literal value of true.

And all decimal numbers in JavaScript are floating points. Then we have Boolean, which we have a true value, which is the literal value of true. Then we have a false value, which is the literal value of false. Pretty easy. Now, there is some nuance here, because we also have something called truthy and falsy. We're not going to get into that here, but whenever we talk about conditions in another episode, we will talk about truthy and falsy values and what exactly they mean. So let's see. That is undefined. That's string. That's number.

Objects, Null, Arrays, Functions3:38

That's string. That's number. That's Boolean. There is another called symbol, and this is something that you will encounter at some point in time, but it's not something that we are really going to dive into here, because it's one of those that it has its uses, and we will encounter it at some point in time, but we don't necessarily need to dive into that. So we'll leave symbol alone for now. And these are the primitive types in JavaScript, and we have more complicated types, which revolve around object. So we can create an object in several different ways, but one of the more common ways is to just use what we call object literal notation. It's just a pair of curly braces, and then we can define the properties and methods for that object.

So we can create an object in several different ways, but one of the more common ways is to just use what we call object literal notation. It's just a pair of curly braces, and then we can define the properties and methods for that object. We can create objects using classes and calling constructors, and we will talk about that when we get into objects. For right now, we're just talking about types. So we have objects, but we also have another kind of object value called null, and there is some confusion between undefined and null. You might think that they're the same, but they aren't, because undefined means that, well, it's a value that is not undefined. We have created a variable, but we haven't given it a value, so therefore it is undefined, whereas null is an object that doesn't exist. I guess that's how we should think about it, because if you inspect a value that's null, it's going to be an object. So null is an object that doesn't exist. Undefined is a value that doesn't exist.

So null is an object that doesn't exist. undefined is a value that doesn't exist. Splitting hairs, but there is a difference. All right, so we have objects. We have arrays, and you are familiar with arrays because arrays are used extensively in php. They're also used in JavaScript, but there is a difference. In PHP, you can create an array that's based upon indexes, and you can also create an array that uses key-value pairs. JavaScript arrays are primarily index-based, and you can technically create an array with key and values, but it doesn't work very well, and it's typically considered a bad practice to use keys and values in an array. So we typically don't do that.

and it's typically considered a bad practice to use keys and values in an array. So we typically don't do that. If we need keys and values, then we use an object, which is something that we will look at at some point. So let me put up here, not associative. And the next type of value is, I'm going to say it is the most important type of value in JavaScript, and that is a function. Because in JavaScript, functions are values, and without functions, the whole language just falls apart. We probably won't go into the weeds as to why that's the case, but functions are very important in JavaScript, and they are their own type. We will, however, spend an episode on functions because they are so important. All right, so earlier, I asked the question, how do we find out what kind of value that we are working with, if it's a variable or if it's a parameter? Well, we don't have type hints, but we do have something called typeof.

Checking Types with typeof6:55

All right, so earlier, I asked the question, how do we find out what kind of value that we are working with, if it's a variable or if it's a parameter? Well, we don't have type hints, but we do have something called typeof. It's an operator that we use, that we follow it up with the variable or the parameter that we want to check the type of. And this returns a string of the type of value. So in this particular case, it would return the string of undefined. For strings, it would return the string of string. For number, it would be number. Boolean would be Boolean. Object would be object. The same is true for null, because null is an object that doesn't exist.

Object would be object. The same is true for null, because null is an object that doesn't exist. An array also returns object. So this is where things can be, well, not as precise as we would want, because you could have an array, and you try to get the type of that array, and it's going to return object. So, yeah, that's just what we have. And then function used to return type object, but now it returns type of function. Now, JavaScript also gives us the ability to create custom types, which is something that we will talk about in a later episode. But for right now, these are the basic types that JavaScript provides. And in the next episode, we will see how they interact with various operators.

And in the next episode, we will see how they interact with various operators.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟