While writing code, it can be useful to keep track of the data types of the variables in your program
but if I wrote a code I already know what type of data I’ve used, can someone give example in which cases need to use typeof operator? I guess maybe in large projects where people use github, but you can always run a search…
you don’t know, it depends on the the argument at function call, so you might want to do a type check to ensure the right data type is passed to the parameter. Or you want different behavior for different data types.
JS is loosely typed and permits us to change the type on a variable on-the-fly. JavaScript uses coercion in a variety of ways, and loose typing plays into this.
1 == '1' // true
JS will coerce one or the other to the matching type before completing the evaluation.
1 === '1' // false
We see above that strict type matching does not coerce.
Strict typing in our code can help to improve performance (one would surmise) since it doesn’t have to do the extra work of coercing types.
One would need to experiment to see if the following (or similar situation) will throw an error, or not.
function getNumber(x) {
'use strict'
return +x;
}
> getNumber('42')
<- 42
> getNumber('n')
<- NaN
We see above that the unary operator, +, does indeed coerce as expected, if it can, else returns NaN.
I’d have to do some more digging to determine other situations that may or may not throw an error.
the exercise asks to reassign newVariable using let newVariable = 1, however I get an error saying that Identifier “newVariable” has already been declared. I thought using let means the variable changes whenever a new one is set?
declaring variable
re-assigning a new value to a variable
when declaring a variable, we use const or let (and sometimes var), which indicates the scope and if the variable can be re-assigned (let, var) or not (const)
which means i also covered the second point, declaring a variable means we can re-assign it:
let newVariable = 'old value'
newVariable = 'new value'
when re-assigning a variable, don’t use let. The scope has already been determined by the initial declaration
Thanks, I was stuck on the same thing. It would be good if this was pointed out in the notes, as even tho I was comparing my code to the “code solution” I didn’t notice the “Let” was missing when reassigning a value
So I tried number 9. Great, now let’s check what happens if we reassign the variable. Below the console.log() statement, reassign newVariable to 1 .
I did newVariable = ‘1’; on the third line and it keeps giving me the x on the question what am i doing wrong
“1” is still a string not a number. When you surround it with single or double quotes it creates a string. to create a data type that is a number it must be a number with no quotes.
Probably a very daft question where I’ve not understood something on the course, but on the excersise, the same call (sorry, very new, may be the wrong term) is used to produce two different answers, IE
I was under the impression that the whole code on the page was rendered together (no idea why), so whatever the variable was last defined as would be the case for the whole page (EG a variable set on line 500 would be able to be called by a console.log() on line 1). Obviously very wrong about this.
Does this mean that the script is read chronologically (IE that until newVariable was a string on line 1 & 2 and a number on lines 3 & 4 & the change happens there)? And if that’s the case (or something else is the case) - what benefit does this have?
Just a bit curious, happily plodding & I’m sure this’ll be resolved at some point in the near future!
Seems you mostly answered your own question? Being able to update variables is very useful, for example we might want to prompt the user for some input, and keep prompting until we have valid input. Then we don’t want to have dozen of variable
That is its nature. Top down, once through. That’s why it is called script. Each line is parsed only after the line or lines before it have already executed. It is then compiled and executed. Any variables that permit state change (they were declared with var or let or (defined directly)) are vulnerable to new value assignment.
Owing that JS is loosely typed, we can assign a new type, not just a new value. It’s very important that we have our variables under close control and also why we shy away from global variables.
We can make JS behave like a program by using event listeners to emulate the REPL nature of the computer. In this way we can revisit functions.