01_Javascript as a Dynamic Typed language
- Source Code: Original code (usually typed by a human into a computer)
- Translation: Converting source code into something a computer can read (i.e. machine code)
- Run-Time: Period when program is executing commands (after compilation, if compiled)
- Compiled: Code translated before run-time
- Interpreted: Code translated on the fly, during execution
Typing
“When types are checked” : "3" + 5 will raise a type error in strongly typed languages, such as
- Python and Go, because they don't allow for "type coercion": the ability for a value to change type implicitly in certain contexts (e.g. merging two types using
+). Weakly typed languages, such as JavaScript, won't throw a type error (result: '35').
- Static: "Types checked before run-time.Static typing can be also considered as limited form of program verification and in a type-safe language, can be considered also an optimization. If a compiler can prove that a program is well-typed, then it does not need dynamics checks, allowing the resulting compiled binary to run faster and to be smaller."
- Dynamic: Types checked on the fly, during execution
- The definitions of “Static & Compiled” and “Dynamic & Interpreted” are quite similar…but remember it’s “when types are checked” vs. “when source code is translated”.
"Type-checking has nothing to do with the language being compiled or interpreted!"
+). Weakly typed languages, such as JavaScript, won't throw a type error (result: '35').- The definitions of “Static & Compiled” and “Dynamic & Interpreted” are quite similar…but remember it’s “when types are checked” vs. “when source code is translated”.
JavaScript is dynamically vs weakly typed language.
Does JS have types?
JavaScript is untyped or that it shouldn’t call its type system types. It doesn’t require you to declare a type when making a variable like in some other strongly typed languages i.e
int x = 10 I would argue that JS does have types.
JS is both dynamically typed and weakly typed.
Statically Typed
JS is not statically typed unless you’re using a language, tool such as Typescript or Flow that compiles to JS code. But we’ll briefly cover it for comparison reasons.
Statically typed means the type is enforced and won’t change so easily. All variables must be declared with a type.
int x = 5string y = 'abc'
Dynamically Typed
Dynamically typed languages infer variable types at runtime. This means once your code is run the compiler/interpreter will see your variable and its value then decide what type it is. The type is still enforced here, it just decides what the type is.
var a = 1 // intb = 'test' // string// etc
Weakly Typed
Weakly typed languages allow types to be inferred as another type. For example,
1 + '2' // '12' In JS it sees you’re trying to add a number with a string — an invalid operation — so it coerces your number into a string and results in the string ‘12’.Primitives
These six types are considered to be primitives. A primitive is not an object and has no methods of its own. All primitives are immutable.
- Boolean — true or false
- Null — no value
- Undefined — a declared variable but hasn’t been given a value
- Number — integers, floats, etc
- String — an array of characters i.e words
- Symbol — a unique value that's not equal to any other value
- Everything else is an Object type.
Why is Null an Object?
The documentation lists it as a primitive type, yet its
typeof returns ‘object’.
Basically, this is a bug that isn’t fixed because it would break existing code. This bug has been around since the first version of JavaScript. The bug comes from the
typeof function in the JS source — Use some pseudo code to simplify it.
Comments
Post a Comment