Fly with code

Get wet inside ocean of code

0%

Language type of JavaScript

Dynamically-typed

JavaScript is a Dynamically-typed language.
But, what is the definition of Dynamically-typed ?
For example,

1
2
let a=1;
let b='blablabla';

Indeed, the real process is below, and you can use typeof to check the type
then you will get

1
2
3
4
5
6
7
8
let a;
console.log(typeof(a)); // undefined
let b;
console.log(typeof(b)); // undefined
a=1;
console.log(typeof(a)); // number
b='blablabla';
console.log(typeof(a)); // string

Means, the type of variable is decided when the value is given to that variable.
This is called Dynamically-typed.

weakly-typed

JavaScript is a weakly-typed language.
And there are two types of coercions under the weakly-typed language

  • explicit coercion

example as below

1
2
3
4
let a=1;
console.log(typeof(a));//number
a='blablabla';
console.log(typeof(a));//string

When a variable is given another type of value, it will undergo explicit coercion.

  • implicit coercion

example as below

1
2
3
4
5
6
let a=1;
console.log(typeof(a));//number
a=a+'';
console.log(typeof(a));//string
a=a*3
console.log(typeof(a));//number

This is called implicit coercion.

Additional Knowledge

你懂 JavaScript 嗎?#8 強制轉型(Coercion)