Voice of a Developer: JavaScript Data Types

JavaScript is a language of the Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript and what mistakes generally developers make and what differences they should be aware of.

Data types

A very common word and ry vebasic in programming language, JavaScript provides different data types to hold different types of values.

Q: How many data types are there in JS?

Ans: There are two types of data types in JavaScript.

Primitive data type

  • String
  • Number
  • Boolean
  • Undefined
  • Null

Non-primitive (reference) data type

  • Object
  • Array
  • RegExp

Q: How do you specify data type in JS?

Ans: It’s simple and could be defined using var keyword only

Ex-

  1. var x=10; // holds number  
  2. var x=”javascript”; // holds string  
As a developer, I am always curious to know the size of datatypes. But there is no sizeof function available which provides me size in JavaScript. But it’s good to know memory consumption when we work on bigger applications.

Download sizeof.js

Download one of the files below and upload it to your web server.

Example-
  1. // define an object  
  2. var object =  
  3.     {  
  4.       'boolean' : true,  
  5.       'number'  : 1,  
  6.       'string'  : 'a',  
  7.       'array'   : [1, 2, 3]  
  8.     };  
  9.   
  10. // determine the size of the object  
  11. var size = sizeof(object);  
  12. //output: size is 92 bytes   
Q: What is the difference in Undefined and Null?

Ans: In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such a,
  1. var TestVar;  
  2. alert(TestVar); //shows undefined  
  3. alert(typeof TestVar); //shows undefined  
  4. null is an assignment value. It can be assigned to a variable as no value:  
  5. var TestVar = null;  
  6. alert(TestVar); //shows null  
  7. alert(typeof TestVar); //shows object  
Objects

Objects are variables too. However, objects can contain many values. Ex-
  1. var person = {age:59, address:"Delhi”};  
Q: Do you know simple way to create objects?

Ans:
  1. var obj={};  
  2. //undefined  
  3. typeof(obj);  
  4. "object"  
  5. Object Properties  
  6. You can access object properties in two ways:  
  7. objectName.propertyName  
  8. or  
  9. objectName["propertyName"]  
Ex-
  1. person.age   
  2. Or  
  3. Person[“age”]  
Object Methods

You access an object method with the following syntax:

objectName.methodName()

Example
  1. var person = {age:59, address:"Delhi", fullName:function(){console.log('John Doe');return 'John Doe';}};  
If you access the property without parenthesis (), it will return the function definition:

person.fullName
  1. // function () {console.log('John Doe'); return 'John Doe';}  
  2. Otherwise it’ll execute the method  
  3. person.fullName();  
  4. // John Doe  
Loop over all properties in Object

We often use FOR statement to loop, but there is another feature, for in,  which is pretty useful to iterate over all properties of Object

Ex-
  1. for(a in person){console.log(a)}  
  2. age  
  3. address  
  4. fullName  
Q: How do you add/delete properties to existing object?

Ans: It is simple in Javascript, by giving a value

Ex-
  1. person.country = “India”;  
To delete you could use delete keyword

Ex-

delete person.age;

Access operator

Goto browser console window and try to access all properties /methods via access operators, ex-

OPERATOR

Q: Can you access a property, which do not exist in Object?

Ans: Yes, you can access a property on an object that does not exist but will result in undefined.

Ex-
  1. person.lastName;  
  2. //undefined  
Q: Can you access a method, which do not exist in Object?

Ans: No, you cannot access a method on an object that does not exist because it’ll yield an error.

Ex-
  1. person.lastName();  
person

Read more articles on JavaScript:

Up Next
    Ebook Download
    View all
    Learn
    View all