25 dynamically typed variables JavaScript

Learn Javascript from Scratch

Last updated on July 13th, 2020 at 11:22 am

This video is taken from the full course that will teach you HTML, CSS, Programming concepts and Javascript .

Transcript :

Hi, and welcome back.

So JavaScript is dynamically typed.

So this means that you can change, you can change the type of a variable in your code.

An example is this.

We have this variable.

We have declared this variable clearly as a string.

Here you can see that this is a string.

So note that when you have strings, you must always enclose them in the quotation marks, this one.

You can use the double quotes or the single quotes.

And this is true of many ‘C based’ programming languages.

So going back to data typing, dynamic typing, we have this variable up here as a string.

Then later on, you can declare the same variable or rather you can give the same variable.

You don’t have to redeclare it.

You can just give the same variable a different value.

So if I come down and I give it a different value and you can see in this case, now it’s a number.

And now that it is a number, we can call it.

And the last variable it’ll give us will be a number and not the string.

And this is not going to give you any error in JavaScript.

In other strictly typed program languages, this would give you an error if you try to give this variable, which was clearly defined up here as a string, and then you come down here and you give it a value which is a number, that would give you an error in other program languages.

But in JavaScript, this is not going to give you any errors or any warnings.

So if I come here on, let me just go here and open Scratchpad on your computer.

And to open scratchpad it’s ‘Shift+F4’ on your keyboard, once you’ve opened Mozilla, once you’ve opened Firefox.

So here I have this variable declared, variable ‘question’, and I have given it an instance of a string.

So note that strings always have this double quotation marks.

So if I come down here, so in the past before I go on, I have gotten back results using ‘alert’.

So if I, if I ‘alert’ this, it’ll bring an ‘alert’ box for me.

But on Scratchpad, you can also just display a value, right there.

So if I say ‘question’, this will give me the value of, of ‘question’.

So instead of alerting it and having to go to the browser to see the results, I can just come here where ‘var question’ has been declared and then I called ‘question’ down here, if I want to find the value of ‘question’.

So if I display that, it’ll give me the value of ‘question’, in within this comment.

So we have declared a variable called ‘question’ and then we have given it a value as a string.

So if I come down here.

Let me just copy this one.


So, right now the variable, this variable is like a ‘string’.

If I come down here and I change the value of this to ‘question = 3’ and then I come and call ‘question’.

I want to know the value of ‘question’ and then I click on ‘display’, it will tell us the value of this variable right now is ‘3’.

So note that this is the same thing, if I was doing an ‘alert’, I would just enclose it, I would enclose this question within the ‘alert’ so that it can give me a value in the results.

So from now on, I will be doing this, I will be mixing between the two.

I will maybe do a ‘display’, maybe do an ‘alert’.

But I wanted you to get comfortable with the ‘alert’ before I moved on to the ‘display’.

So this is only true here on, on Scratchpad.

If you’re using anything else, you have to use another way to display your results.

So for example if I say, ‘2+2’, I can just come here and click ‘display’, and it will give me the results within these comments.

So that’s why, from now on I will be showing my results using ‘display’ and once in a while maybe with the ‘alert’ as well until you get to a point where we’ll start using JavaScript within HTML.

So in this video, we have seen that JS is dynamically typed.

That means you can change the value of a variable to another.

If this was maybe a number, down here you can also give ‘question’, this variable ‘question’, you can give it a different data type.

Like in this case, we took it from a string and then we also gave it a value of a number.

And JavaScript, the JavaScript interpreter did not complain at all.

This is not true for other programming languages which are strictly typed.

So that’s it for this video.

I’ll see you in the next video.

If you have any questions, let me know.

Click the button below to watch this full course:

Comment Here