You are on page 1of 2

In many programming languages when you create a variable, you don't just give it a name but you must

also say exactly what type of information is going to be stored in that variable. You must decide
beforehand if it's going to store an integer, meaning a whole number without anything after the decimal
point, or perhaps a floating-point number which might have a value after the decimal point or perhaps you
need to store a single character, just one letter, or perhaps multiple characters, what's called a string, or
is it a Boolean value, and that just means a value that can only be true or false, or it could be even more
complex type of data, but you have to choose that data type.
And once you have chosen it you are not allowed to change it, and that's what's known as a strongly
typed language. You can create as many variables as you want but each variable must be of
one particular type and that's actually enforced and it can cause your program to crash if you try to put the
wrong type in a variable that wasn't designed for it, but in JavaScript we don't do that. JavaScript is what's
called a weakly-typed language. We don't make a variable that's only for integers or a variable only for
strings.
We just create a generic variable, an empty container, and then we can simply put whatever type of value
we want in it. Now if I create this variable without giving it a value, it starts off as what JavaScript calls
undefined.However, then I can use the name of the variable and the equal sign, or more formally the
assignment operator, to set the value of myVariable = 200. Now if I wanted to, right after this I could use
the same format and set it equal to a string. That just means one more characters strung together.

Now we need to use the double quotes here to say where the string begins and where it ends. Inside
those double quotes it can contain spaces and special characters. Now in JavaScript you can use either
double or single quotes to mark the start and end of a string. But don't mix them. Don't start with a single
quote and close with a double quote. Now I tend to use double quotes because that's more common in
other languages but you will see both ways. And we can also store what are called Boolean
values. Boolean just means a value that's either true or false.
We can use the word true or the word false and written this way, they do not need the quotes around
them. JavaScript understands the word true and false written all lowercase. They are considered part of
the language. So we can create a variable as undefined, then put a number in it, then put a string in it,
then a Boolean. Now that doesn't mean JavaScript doesn't care about the type of data you have. It does.
It treats numbers differently from strings and strings differently from Boolean values. But any JavaScript
variable can hold any of these values and even more complex ones besides, like arrays and objects and
functions, but we'll get into those later on.

Now you might think, well it sounds much easier to be a weakly-typed language where you can put
anything anywhere. One issue with a weakly-typed language like this is there is no guarantee that the
variable you think you have contains the right kind of data. Most of the time when you're programming,
you won't need or want to change the type of data that a variable stores. If you have a variable that
you've called high score, well you would expect it to have a number in it, not the word pomegranate or the
value true. If you have a variable called firstName you don't want it to contain the value of 74.5. And there
is nothing that would prevent that in JavaScript, but in other languages strong typing can have an
advantage by providing internal rules that stop a lot of common errors from happening.
Now of course, the point of making any variable is that we are going to use them, we are going to
manipulate them, to ask questions of them, but that allbegins by knowing how to make them.

You might also like