Chapter 3 - Types, Values and Variables
What are the different categories of objects in Javascript? Ans. Primitive:
undefined
,null
, String, Boolean, Number Object type: Array, Object, Function, Date, Error, RegExState some method less types in JS. Ans.
undefined
andnull
are the two types.Give examples of mutable and non-mutable object types. Ans. Mutable: Object, Array and Non-Mutable: Boolean, Numbers, Strings
Are Strings mutable in JS? Ans. No they are non-mutable objects.
var str = 'tushar' console.log(str) //tushar console.log(str[2]) //s str[2] = 'x' console.log(str[2]) //s console.log(str) //tushar
How can you specify integer literals? Ans.
//Decimal var e = 10 //Hexadecimal var f = 0xa //Octal (not supported by ECMAScript standard) var g = 07 //Exponents to the base 10 var h = 2e5 // Represents 2,00,000
How to check if the number is a finite number without using isNaN or isFinite methods? Ans. No, because of its unusual behavior.
NaN == NaN //false x = NaN x != x //true
What happens to precision when you use floating point numbers such as .1, .2 etc? Ans. Binary floating point numbers have to round of such values.
var x = 0.3 - 0.2 // 30 cents minus 20 cents => 0.09999999999999998 var y = 0.2 - 0.1 // 20 cents minus 10 cents => 0.1 x == y // => false: the two values are not the same! x == 0.1 // => false: .3-.2 is not equal to .1 y == 0.1 // => true: .2-.1 is equal to .1
What is the default unit of difference of two dates? Ans. Milliseconds.
How are strings encoded in Javascript? Ans. Javascript uses UTF 16 encoding. In a string each char is represented by a 16 bit value. Incase you want to store a value which is bigger than 16 bit you can use a sequence of multiple 16 bit values. Overall the string manipulation methods apply on 16bits only and not on characters. The length property will also tell the
How to represent a string literal in multiple lines? Ans. Use backslash ‘’ for Eg.
var x = 'tushar\ mathur\ is\ a\ good\ boy'
How is a NULL character represented in Javascript? Ans. Null character can be represented using ‘\0’ or \u0000
How are RegEx represented in Javascript? Ans. RegEx are not primitive types but can still be initialized using literals with forward slashes, for e.g.
var pattern = /[1-9][0-9]*/
What are falsy values in Javascript? Ans.
undefined
,null
, 0, -0, NaN, “” are all falsy values. When equated to false the result is always true. Other than these values everything else is javascript is a truthy values.What is the difference between
null
andundefined
? Ans. Comparison ofnull
andundefined
-- Both are falsy values.
- Both represent absence of values.
- Both are method less types.
typeof null
returnsobject
where astypeof undefined
returnsundefined
.- Undefined has a deeper meaning - represents that the variable has not be assigned a value and is generally considered as a system level absence of value.
null
can be considered as a program level absence of value.
What is a method? Ans. When functions are referred as properties of objects, they are called as methods.
If string, number, boolean are of primitive types then why do they have methods and properties associated with them? Ans. Whenever a property is accessed in any of the string, number or boolean type object the interpreter creates a temporary object using there constructors. It then refers to the properties and methods and later discards that object.
What will be the output of the following code?
var s = 'tushar' s.len = 4 var t = s.len console.log(t)
Ans. The output will be
undefined
. Because strings are primitive type objects and in the second line when thelen
property is accessed it is done so by creating a temporary object which is later discarded. In the third line whenlen
is again accessed a new temporary object is created usings
string which has no information about thelen
property.What will be the output of the following code?
x = 10 y = new Number(10) console.log(x == y, x === y)
Ans. The output will
true
andfalse
. This is because thetypeof x
andtypeof y
are different as x is a primitive typenumber
and y is of object typeobject
.Compare and contrast primitive and object types. Ans.
Primitive:
null
,undefined
,number
,string
,boolean
- They are compared using only values and thus are also called as value type
- They are immutable. You can not modify the values but you can create a copy.
Object:
Function
,Object
,Array
,Date
,Error
,TypeError
,RegEx
- They are reference type and two objects are equal only if their references are equal.
- They are mutable.
Can
undefined
andnull
be type casted toObject
type? Ans. No, it will throw aTypeError
exception.How are arrays converted to strings? Ans.
javascript [].toString() // => "" [1].toString() // => "1" or "a" [1,2,3].toString() // => "1, 2, 3" or "a, b, c"
How are different types of values converted to number type? Ans. _ Strings that can be parsed as numbers are converted to numbers else produce a
NaN
value. Empty strings are 0. _true
is 1 andfalse
is 0. * Arrays with single elements are parsed and the first element is parsed again to check if it can be a number elseNaN
is returned by default.How are primitive to Object conversion carried out? Ans. Simply using there wrapper objects.
Is is legal to use a variable without declaring it? Ans. It is not allowed in Strict mode. In case you still do it you will assign the variable as a property to the global object
What happens in the following code -
a = 100( (function() { a = 200 b = 300 })() ) console.log(a + b)
Ans. The answer is
500
. Because if we use a non declared variable anywhere in the code it is assigned to the Global object as a property.What is the output of the following code -
var _whatIsMyName = function() { console.log(this.name) } var item = { name: 'Grandpa', whatIsMyName: _whatIsMyName, child: { name: 'Father', whatIsMyName: _whatIsMyName, child: { name: 'Son', whatIsMyName: _whatIsMyName } } } item.whatIsMyName() item.child.whatIsMyName() item.child.child.whatIsMyName()
Ans.
Grandpa Father Son
Determine the output of the following code -
var b = 'mangoes' var c = 'apples' ;(function() { console.log(b, c) var b = 100 c = 30 })()
Ans. The output will be
undefined
. This feature of Javascript is called as Hoisting. Accessibility to variables which are yet to be initialized.
More on Type Conversion later, till then take a look at this snapshot from the book.