Wern Ancheta

Adventures in Web Development.

What I Learned From AppendTo JavaScript 101

| Comments

I started learning JavaScript at appendTo developer learning center and found out things about JavaScript that I didn’t know before. Here are some of those:

  • Automatic type coercion – concatenating a string and a number doesn’t result in an error, the type of the other variable is converted to the type of the other. Here’s an example:
1
2
console.log(25 + "yoyos");
//output: "25yoyos" 

If for example the result of an operator is not a number then we get NaN.

1
"hello" - "world"; //output: NaN
  • There’s only one number data type in JavaScript which means that 52 and 52.0 are the same.
  • JavaScript is loosely typed which means that a variable can represent any data type at a given time. Here’s an example:
1
2
3
var x = 2; //number
x = true; //boolean
x = "Im a string"; //string

In the example above we changed the type of the variable x from number, to boolean and finally to a string. This is perfectly fine in JavaScript unlike in other languages where you have to explicitly specify the type and you will have to stick with that while the program executes.

  • There are 7 falsy values:
1
2
3
4
5
6
7
0
-0
null
undefined
false
NaN
""
  • Force conversion from a specific type to boolean. This will most likely be used in determinining whether a variable contains a truthy or falsy value.
1
2
!!5; //output: true;
!!undefined; //output: false;
  • typeof operator allows you to determine the type currently represented by a variable. Here’s an example:
1
2
3
4
5
6
typeof "Im a string"; //output: "string" 
typeof 2; //output: "number"
typeof 2.2 //output: "number" - like I said earlier there's only one number type in JavaScript
typeof null //output: "object" - yes JavaScript is pretty nasty sometimes
typeof NaN //output: "number" - Not a Number has a type of Number, pretty cool
typeof undefined //output: "undefined" - pretty reasonable I guess?

Comments