text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
And JavaScript is fine with that just
because it has those dynamic types. | 787.12 | 4.17 |
And there a certain
number of primitive types. | 791.29 | 2.13 |
Primitive types are types that have
no methods, and they're immutable. | 793.42 | 4.36 |
And so undefined, null are two of them. | 797.78 | 2.54 |
Boolean. | 800.32 | 0.81 |
Everybody should know
Boolean true or false. | 801.13 | 2.86 |
Number 1, 2, 3, negative 1, 2, 3. | 803.99 | 3.55 |
It has no float, so 0 and
0.1 are both of type number. | 807.54 | 6.79 |
String, which are the
things between the quotes. | 814.33 | 6.45 |
And then symbol, which is
something that's new in ES6, | 820.78 | 2.43 |
but we're not going to talk about
it, nor are we going to use it, | 823.21 | 5.13 |
and then everything else is an object,
and we'll talk a little bit more | 828.34 | 3.382 |
about that later. | 831.722 | 0.708 |
Cool and so a lot of languages
have this thing where | 836.24 | 5.49 |
you change one type to another type. | 841.73 | 1.87 |
So since JavaScript
is dynamically typed, | 843.6 | 2.18 |
we have this thing called typecasting. | 845.78 | 3.02 |
So coercion is the act of changing
one type to a different type, | 848.8 | 3.67 |
and there's two different ways
you can coerce these variables. | 852.47 | 2.88 |
So say, as an example, we
have this const called x | 855.35 | 3.18 |
and we give it a value 42. | 858.53 | 2.04 |
Say I wanted to change
that value to a string. | 860.57 | 2.22 |
There are a couple
different ways I can do it. | 862.79 | 1.24 |
There's explicit coercion. | 864.03 | 1.76 |
There's implicit coercion. | 865.79 | 1.3 |
So explicit is, hey, I'm
just going to tell you | 867.09 | 2.57 |
exactly what I want by wrapping
it with the type that I want, | 869.66 | 4.29 |
and give it to me. | 873.95 | 1.17 |
And so that's being very
explicit with what I want. | 875.12 | 2.19 |
And so the example of
that would be say I | 877.31 | 1.83 |
want to get a string from that value x. | 879.14 | 2.22 |
I just wrap it with capital string,
and it pops out 42 with a string. | 881.36 | 5.39 |
There's also a way of
doing it implicitly, | 886.75 | 1.75 |
which is I'm going to rely
on the behavior of JavaScript | 888.5 | 3.24 |
in order to get this to a string. | 891.74 | 1.69 |
And so say I want to
add 42 to empty string. | 893.43 | 4.859 |
That wouldn't really make sense
if I wanted to have a number, | 898.289 | 2.541 |
but since 42 is easily
castable to a string, | 900.83 | 2.49 |
I can say, hey, give me
42, add an empty string, | 903.32 | 3.39 |
and I expect to get back
something of type string. | 906.71 | 2.85 |
That is called implicit coercion. | 909.56 | 3.61 |
And so how might we go
about comparing-- oh, yeah. | 913.17 | 2.166 |
Question. | 915.336 | 0.5 |
AUDIENCE: Is the string passing
behind the same ex.2 string? | 915.836 | 3.753 |
JORDAN HAYASHI: Yeah, so another
way would be to invoke that. | 919.589 | 2.541 |
So the question was,
what about ex.2 string? | 924.92 | 3.84 |
And so yes, another way to
get from a number to a string | 928.76 | 3.69 |
would be to invoke that method on
the string or on the number, sorry. | 932.45 | 4.99 |
Cool, and so how might we
go about comparing values? | 937.44 | 4.16 |
So in most languages you can compare
values with the double equals, | 941.6 | 3.39 |
but JavaScript has this
thing called triple equals. | 944.99 | 2.761 |
So there are two different
ways of comparing equality. | 947.751 | 2.249 |
There's double equals and
there's triple equals, | 950 | 2.26 |
where double equals will
actually coerce the types, | 952.26 | 2.48 |
and triple equals require
that the types match. | 954.74 | 4.05 |
And so let's play with
that a little bit. | 958.79 | 1.905 |
So say I have this value, and I want
to know exactly what type that is. | 966.88 | 9.51 |
So there's this operator called typeof
in JavaScript where I can invoke this. | 976.39 | 4.62 |
And that will give me the type
of whatever that variable holds. | 988.77 | 8.99 |
So if I were to run this code,
then it would say number, | 997.76 | 6.05 |
because that is the type. | 1,003.81 | 1.8 |
So right now in order to
execute my JavaScript, | 1,005.61 | 1.98 |
I'm using this thing called node,
which, as mentioned earlier, | 1,007.59 | 3.45 |
is basically a command line runtime for
JavaScript, which is built off of V8. | 1,011.04 | 5.2 |
Any browser has a console where you
can also just type JavaScript directly | 1,019.74 | 3.09 |
in there. | 1,022.83 | 1.05 |
So if I were to open up the tools on
Chrome, which is my browser of choice, | 1,023.88 | 6.969 |
I actually get this, which
is a JavaScript console built | 1,030.849 | 9.441 |
into all of these browsers. | 1,040.29 | 1.613 |
So if you guys are using Chrome at
home, and you want to follow along, | 1,041.903 | 2.916 |
you're welcome to open up
the developer tools here. | 1,044.819 | 3.991 |
Go to console, and you have your
own JavaScript interpreter here. | 1,048.81 | 3.93 |
So say I wanted to do that thing
where I do const x equals 42, | 1,052.74 | 5.944 |
and I wanted to get the
type of that variable. | 1,058.684 | 1.916 |
I can just do typeof x, and
it will output that number. | 1,060.6 | 4.62 |
So there's a little bit of a caveat with
this, where this might surprise you. | 1,072.74 | 4.52 |
So who thinks they can
guess what this will output? | 1,083.33 | 4.24 |
First of all, what should it output? | 1,087.57 | 1.725 |
So if we remember back a few slides, we
talked about all the different types. | 1,092.71 | 6.4 |
One of them is undefined,
one of them is null. | 1,099.11 | 2.18 |
And say I want to get typeof null. | 1,101.29 | 6.99 |
So what should it output? | 1,108.28 | 3.191 |
Yes? | 1,111.471 | 0.499 |
AUDIENCE: String. | 1,111.97 | 0.845 |
JORDAN HAYASHI: String,
why would you say string? | 1,112.815 | 2.041 |
AUDIENCE: Because [INAUDIBLE]. | 1,116.902 | 1.968 |
JORDAN HAYASHI: That's a good guess. | 1,124.53 | 1.5 |
So basically, the answer was so if you
can console log it must be a string. | 1,126.03 | 5.34 |
And while that is correct, most of these
types can actually be cast to string. | 1,131.37 | 8.4 |
So we talked about implicit
versus explicit coercion. | 1,139.77 | 3.12 |
And the way that console
log works is it actually | 1,142.89 | 2.64 |
will turn these values into a
string in order to console.log them, | 1,145.53 | 4.68 |
but it doesn't necessarily mean the
typeof that value itself is a string. | 1,150.21 | 4.99 |
Yeah? | 1,155.2 | 1.48 |
Yeah, so the null. | 1,156.68 | 2.08 |
So we would expect the typeof null
to be null, since null is actually | 1,158.76 | 3.57 |
a primitive type. | 1,162.33 | 1.69 |
However, this actually returns object. | 1,164.02 | 4.61 |
So JavaScript does have
some strange behaviors. | 1,168.63 | 3.64 |
This is one of them. | 1,172.27 | 2.06 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.