Instead of clear "length is not a function" we'd have something like "I'm sorry Dave, I'm afraid I can't do that".
If there wasn't any notion of types in JavaScript, we would either see some bizarre unexpected behavior or some mystic error. When we've tried running a number as if it was a function, JavaScript started complaining and we could see the problem and fix it. NaN means "not a number", but the type of NaN is "number". These are some of the types in JavaScript - number, string, boolean. typeof 42 // 'number' typeof 3.14 // 'number' typeof NaN // 'number' typeof ' Berry ' // 'string' typeof true // 'boolean' typeof false // 'boolean'Ĥ2 and 3.14 are obviously numbers, some combination of letters in quotation marks is a string, and true and false are boolean. Typeof is a special operator that returns a string indicating the type. Function is one type, Number is another type, and you can't just use a number as a function. As you know by now, JavaScript somehow can see the difference between types. This is a general term, and different programming languages deal with types in different ways. In programming "type" is a classification of information. You probably wanted to ask your roommate.
Type Error is like asking your cat to do the laundry. JavaScript interpreter wouldn't tell you what the thing is, but it will tell you what it isn't. This is a Type Error - the type of the thing you've used is wrong. And this is exactly how JavaScript will complain: → node test.jsĪt Object.Module._extensions.js (module.js:580:10 )Īt Function.Module._load (module.js:439:3 ) Numbers are not functions, not boxes that do stuff. But wait! length is not a function! It's a number. Then, on the second line, we call a function length and pass an argument - number 54. Remember, it's like giving a name to something, in this case - giving a name length to a number 12. Look at this code: const length = 12 const num = length ( 54 ) įirst, we create a constant. The next kind of error is when you confuse one thing for another. There are different kinds of errors, and I want you to recall one particular kind.
The stronger the typing system is - the stricter the rules are.Įxplicit conversions in JavaScript Number ( ' 590 ' ) // 590 Number ( ' aaa!! ' ) // NaN Boolean ( 1 ) // true Boolean ( 0 ) // false String ( true ) // 'true' String ( false ) // 'false' String ( ' 44843 ' ) // '44843' Optional readingįew lessons ago we were talking about errors and how to deal with them. It has a notion of types, but it's relaxed about them, and can treat values somewhat arbitrary. JavaScript assumes and converts types automatically a lot: 4 + ' 7 ' // '47' 4 * ' 7 ' // 28 2 + true // 3 false - 3 // -3 Statically typed languages check the types and look for type errors during compile time.ĭynamically typed languages check the types and look for type errors during runtime.Īnother way to think about it: static typing means checking the types before running the program dynamic typing means checking the types while running the program. This process is called compilation, and the period of time this happens is called " compile time".Īfter compilation is over, the program is launched, and the period it's running is called " runtime".
The code you write is usually converted into some other form that a computer knows how to run. log ( a ) // undefined typeof a // 'undefined' Dynamic vs Static typing Type of this variable is undefined: let a console. NaN means "not a number", but the type of this value is number.Ī variable without a value has a special value undefined. typeof returns a string indicating the type: typeof 42 // 'number' typeof 3.14 // 'number' typeof NaN // 'number' typeof ' Berry ' // 'string' typeof true // 'boolean' typeof false // 'boolean' JavaScript has a notion of types: numbers, strings, functions, boolean, etc. It shows you what happens in JavaScript's mind when it deals with types: 1 = ' 1 ' // false 1 = ' 1 ' // true true = 1 // false true = 1 // true Lesson notes Types in JavaScript There is a relaxed comparison in JavaScript - with to equal signs. This comparison is strict: are those two things precisely the same? In this course we've been comparing things using three equal signs: a = b 12 = 12 In the article you will find a solution to this problem.
Video may be blocked due to browser extensions.