Sunday, August 31, 2014

Primitive Data Types

Variables are Case Sensitive

Primitive Data Types

Number

  • this includes floating point numbers as well as integers
  • Infinity
  • NaN
infinity - infinity is NAN
10 * "f" is NAN
1 + 2 + NAN is NAN

String
var s1 = "one";
var s2 = "two";
var s3 = s1 + s2;
s3 is onetwo

Boolean
  • can be either true or false
If you quote true or false, they become strings
Understanding how any value converts to a boolean is important. The following values convert to false
  • The empty string ""
  • null
  • undefined
  • The number 0
  • The number NaN
  • The boolean false
Undefined
when you try to access a variable that doesn't exist, you get the special value undefined.
The same will happen when you have declared a variable, but not given it a value yet.
>>> var i = 1 + undefined; i;
NaN
>>> 1*undefined
NaN
>>> !!undefined
false
>>> "" + undefined
"undefined"

Null
It means no value, an empty value, nothing.
The difference with undefined is that if a variable has a value null, it is still defined, it only happens that its value is nothing.
null is an object

>>> var somevar = null
>>> typeof somevar
"object"

>>> var i = 1 + null; i;
1
>>> 1*null
0
>>> !!null
false
>>> "" + null
"null"

typeof Operator

To know the data type of a variable or a value, you can use the special typeof operator.
This operator returns a string that represents the data type.

Logical Operators

!, &&, ||

Order of precedence (!, &&, ||)

Oddities

if JavaScript encounters a non-boolean expression as an operand in a logical operation, the non-boolean is returned as a result.

>>> true || "something"
true
>>> true && "something"
"something"

Comparison

==
===
  • both value and type equality

!=
  • Returns true if the operands are not equal to each other (after a type conversion)

!==
  • Non-equality comparison without type conversion
>
>=
<
<=

>>> NaN == NaN
false

No comments:

Post a Comment