FAQ: Variables - typeof operator

This community-built FAQ covers the “typeof operator” exercise from the lesson "Variables ".

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise typeof operator

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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…

1 Like

here is a piece of code:

const example = par => {
    console.log(par);
}

what data type is par?

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.

4 Likes

It appears that Javascript is not a strongly type-checked language, ie, you can stick anything inside a var and compiler wont complain?

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.

Strict mode

2 Likes

6 posts were split to a new topic: Why is the typeof operator formatted differently from other operators?

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?

3 Likes

there are two parts to this question:

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

5 Likes

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

2 Likes

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.

Apposed to newVariable = ‘1’ use newVariable = 1

2 Likes

Why does the line, typeof newVariable, return Number after it has been assigned 1? Shouldn’t it be an integer?

Js doesn’t have an integer data type:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

2 Likes

Hello,

Why would we use “typeof” operator for?

Thanks

to determine the type of something, function/keywords generally have logical descriptions

Hello!

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

let newVariable = ‘Playing around with typeof.’;

console.log(typeof newVariable) // returns ‘string’

newVariable = 1

console.log(typeof newVariable) // returns ‘number’

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!

Ta

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

Ah OK I see! Sorry, think I was getting confused about where this code was being used. Thanks for the speedy answer!

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.

Is there any operator that specify types of variable, especially number.

I’ve learnt python before and in python there is a function to check the type of variable

a = 3
type(a)
#output <class int' >

so 3 will be considered as int and 3.4 as float.

but if I use console.log(type of a) it will give an output number for both 3 and 3.4