Saturday, September 22, 2012

JavaScript- Basic Concepts

Java Script is not a subset of Java, its completely different language. It is scripting language. JS programs are delivered as source code, it is never compiled(like any other language). It also provide loose typing, although its always preferred to have strong compiler check while you program. There are no classes in JS, there are only objects. IT also supports lambda, which means function as first class member. There is not linker in JavaScript thus all variables are combined in common namespace(global).

JS has following types of values -
Number
String
Boolean
Objects
null
undefined

Number -
There is only one Number type in JS, no integer.
it is represented as 64 bit floating point also known as IEEE -754 (double)
Decimal arithmetic is not good in JS, which means
0.1 + 0.2 == 0.3 -> false. Thus while critical operation always convert the number to integer and then apply arithmatic.
Special type named as NaN(Not A Number).
It is result of undefined ot erroneous operations. Any operation having NaN as input will result as NaN. NaN is not equal to anything, not even equal to Itself. But if we check the type of NaN, it is Number.

Number function - Number(value) -Converts value into number.  It produces NaN if it has a problem. It similar to + prefix operator.
+"42" -> 42
Number("42") -> 42

parseInt(value,10) - specialized in converting values into integers. It stops at first non-digit character. The radix value is required.
parseint("08") - > 0 treated as octal
parseInt("08",10) -> 8

String -
It is sequence of 0 or more 16-bit characters. There is no separate character type in JS. String in JS are immutable. String literals can use single or double quotes, both are same.
String function - String(value), convert value into string(say number).

Boolean - true and false
Boolean function - Boolean(value), convert value to boolean.
returns false for falsy values and true for truthy values.
Here are the falsy values - false, null, undefined, "", 0 , NaN
All other values are truthy, including all objects even "0" and "false"(as string values)

null- value that is nothing.
undefined - default value of variable.

EVERYTHIN ELSE IN JS IS OBJECTS.

No comments:

Post a Comment