Primitives Types in Javascript

If you are familiar with Java then you know that you have to apply a type to a variable.

For example :

String goodmorning = 'goodmorning';

Integer age = 2;

Double price = 2.6;

So this is the concept of typing and Java is heavily typed programming language. The reason that we have types is because the language knows what to do with our variables. If we will say to add two numbers the language knows how to use addition and if we will add two strings the language knows that has to concat them.

Javascript on the other hand uses Dynamic typing and that means that Javascript engine will choose the best type for our variable.

Here is the list of Javascript's Data types. Why call them primitive types also because they are just data and not objects with properties(except Object):

Boolean => var x = true; , var y = false;

Null => var x = null;

Undefined => var x = undefined;

Number => var x=1.2, var y=1;

String => var x="Hi";

Object => var x= {personOne:"John", personTwo:"Mary"}



Let's see some examples of every data type:

Boolean

var person ={age:23};

var maxAge = 23;

var entrance = false;

if(maxAge === person.age){

entrance = true;
return entrance;
}

else{
return entrance;

}

Null and Undefined

Javascript is very flexible with regards to checking for "null" values. So in case you are searching for an empty string you can just use:

if(!pass || !email){

return something;

}

and will check for empty strings (""), null, undefined, false and the numbers 0 and NaN

BUT if you are checking specifically for null then you have to use this:

if(pass=null || email=null). This test will ONLY pass for null.

the same goes to undefined etc...

Number

With this data type you just state that the variable is a number and It can be an integer, a float, a BigDecimal all at the same time.

One helpful function in JavaScript that relates to the Number data type is the isNaN() function and It's used to see If the variable is a number or not.

String

In this part we have to say something very strange about primitive types. Although the primitive types cannot have properties or methods (because they are not objects), methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.(see https://www.w3schools.com/js/js_string_methods.asp )

This is very confusing and chaotic but for the moment lets just say that primitive types can have methods and properties and String type have many(maybe the most) methods and properties,

like indexOf, search, concat, slice etc.

Also you can think of Strings as a sort of “catch-all” for all the data types. What I mean by this is that if the JavaScript engine can’t figure out what data type to assign your variable, it’s probably going to just assign the String data type to your variable.

Object

In object you can assign properties with two main ways.

The first one is to declare an object and use dot notation:

var Person = {};

Person.age=32;

Person.name='Joe';

console.log(Person.age) //outputs 32

console.log(Person.name) // outputs Joe

and the second one is to use JSON like:

var Person = { age:32, name:'Joe'};

Σχόλια