DATA TYPES IN JAVASCRIPT
DATA TYPES
Primitive Datatypes:
String : anything which is enclosed in double quotes or single quotes is called a string.
example: const name ="preetha";
Number : there is only one type of number which is a number .it may be with or without decimals.
example: const books = 10;
Boolean: it has only two values . true or false .
example: const issleeping = True;
Undefined: if anyone creates a variable and don't set anything to it then it is called undefined
example : const exam
Null : it contains a value of nothing.
example: const exam= Null;
Non-primitive Datatypes:
Function : a block of lines to perform specific task.
example: multiplication of two numbers using function
function function-name(parameter1,parameter2)
{
return parameter1*parameter2
}
OBJECTS
Object is the building block of java script and almost everything in java script is an object.
Object is a collection of data items or a collection of functionality.
Object in java script is created using a semicolon and curly brackets.
Inside every object there should be properties and values.
syntax:
object name
{
property : value,
}
EXAMPLE:
const person
{
name :"preetha",
gender : "female",
};
In this example the right side is the property and left side is an value.
The value in the object can be of any type it can be a string ,function ,boolean arrays etc...
The property should not contain spaces
The other thing we notice in the object is a comma , adding a comma after the last property in the object ,even if nothing comes after it is called TRAILING COMMA or TESTING COMMA.
If you want to add something new to the created object you can add like
person.interest = "Web development"💓
ARRAYS
An Array is a collection of items enclosed within square brackets[].
Each thing in an array is called as an item and its position is called an index.
The number of items in an array is called ArrayLength.
Each item in an array can be of any type for example it can be a string ,number ,boolean etc.
syntax:
const array=[];
EXAMPLE:
const food=["rice","sambar","chapathi","dal"];
If you want to access the items inside of an array you can use it like this:
console.log(food);
console.log(food[0]);
console.log(food[1]);
If you want to know how many items in an array you can use .length to know it
eg: console.log(food. length);
Note: the item in an array starts from zero but the .length to check how many items are there in an array is not zero based.