Primitive Data Types

JS Intro

<script>     
// Two Primary Way to Output Java Script

document.write();
console.log();

</script> 

 Data Types

 There are 6 Data types:
1. Number : int & float(with decimal point). Technically all numbers in javascipt are floats.
2. String : text within quots.
3. Boolean: true & false  // dont use qutation marks with this
4. Null : exist but not assign    //var item = null;
5. Undefined : decare but not assign. ex. var item;  //  document.write(typeof item); output is "undefined"
6. Object : discuss later

First five are primitive data types.

What is the variable?

A variable is a word or collection of keyboard character used to store data.

var  myValue= 100;  // use var to declare variable. No whitespace alloweded.
var myText= "Write anything here within quotes"; // use camelcase for variable name

docuement.write(myValue);  // output is 100

 Comments:  

  // single line

/*  Multiline
 comment */ 

 Operators 

Its JS general mathmatics. 
1. Arithmetic Operators : addition, substraction, multiply, divide.
2. Modulas: % reminder after division.   // 5%3    output is 2
3. Increment/ Decrement: ++, --   


Here is the link to check by yourself https://codepen.io/preptuts/pen/LgPyxb



 // var num= 5; document.write(num++); // output : 6
               // var num= 5; num *=5;  document.write(num); // output : 25
              //var num= 5;  num += 10; document.write(num); // output : 15
             //var num= 5; num %= 3; document.write(num); // output : 2
4. Comparision Operator:  
a)  ==   
b) ===  (exact with type) 
c) != 
d) !==
e) <
f)>
g)>=
h) <=
     // var num1= 5; var num2=5 ; document.write(num1==num2); // output : true
   // var num1= 5; var num2="5" ; document.write(num1==num2); // output : true
  // var num1= 5; var num2="5" ; document.write(num1===num2); // output :false


5. Logical Operator: a) !(Logical NOT) b) && (LOgical AND) c) || (Logical OR)
    These operator workes with boolean values
       // var item= true; document.write(item == !false) ;   // outupt: true
// var ex1=6; var ex2=5; document.write(ex1 != ex2); // output true
     // var item=  !!true; document.write(item) ;   // outupt: true. using !! towice return orignal value
in logical && true only if all oprands are true, but in || OR either one of oprands are true then return true.

// var num1= true; var num2=true ; document.write(num1  && num2); // output : true
// var num1= true; var num2=false ; document.write(num1 || num2); // output : true

String

var text1= "hello"; //double quots
var text2= 'world'; // single quots

document.write(text1+text2);    // use + to combite two strings in java script

Converting Non string variable to string

var num= 200;
var result = num + " ";
document.write(typeof result);  // string

Converting String to number


var num= 200;
var text= "10";
var result = num * text;
document.write(typeof result);  //number


Escaping String
 var text= ' I don\'t care of it';

How strings evaluate to boolean value

Empty strings evaluate to false where non empty stings evaluate to true.

var stringtext=" lorem text";
var emptystring= " ";
document.write(!!stringtext); // true
document.write(!!emptystring); // false

Manuplating String with string method
toUpperCase(), toLowerCase(), slice(), charAt() 

document.write("Hello World".toUpperCase()) ;
var stringtext="Hello World";
var result= stringtext.toLowerCase();
document.write(result); 
document.write(stringtext.slice(0,5);  // use to extract part of string here output worl
document.write(stringtext.slice(-5,-3);  // output oll
document.write(stringtext.charAt(4);  // output o

whitespaces in string consider character while counting 
check by practical here https://codepen.io/preptuts/pen/BqaxOQ?editors=0010

Numbers

var num1= 20;    // whole no
vae num2= 20.30 // fractional no
var num3= 5e2;   // 500

javascript has 64 bit floating point number type
var x= 0.1 * 0.2;
document.write(x); // 0.200000000000000004


var x= 10 * "hello" ;
document.write(x); // NAN  not a number

NAN is  of the data type number
 

For Hexadecimal Number start with 0x. 0xf means 15. For octal number start with 0.
Manipulating number with the method

toFixed(2)    //round of the value

The value that can be placed in the parenthesis is also called arguments.

toPresicion(2)    //round of the value
other method;
Math.ceil();                 // round decimal no to next nearest value
Math.round();            // rounded integrer to orignal value
Math.floor();              //down version to nerest integer
Math.max();                // output highest no from list
Math.min();               // output lowest no from list
Math.random() * 5;   // random value between 0 to 5



To do practical live here is the link: https://codepen.io/preptuts/pen/aRdWBY?editors=0010


Conditional Statements

var item="food";
if(item = = = "food")
{
    alert("I am food");
}

if we write item= true then  use if(true). no need to compare like ==true.


Here is the link for practical: https://codepen.io/pen/?editors=0010
Ternary Conditional statement
10>100? alert('yes'): alert('no');



Loops

Block of code that repeated over and over.  while, do while and for loop.
while(){

}
Using Do While Loop 

Using For Loop

Here is the link to practically check it: https://codepen.io/pen/?editors=0010

Nested For Loop


Loop Inside another loop.





Array

is the variable that stores multiple pieces of data & access those pieces of data individually.

here is single and multidimentional arrays.



 Arrays Method

arrayname.length          // to get no of items in an array
firstarray.concat(secondarray)           // to combine two array
Splice()         // used to add new strings in an array
sort();              // sort the array data in one digit only
reverse()          // revese the array data
pop();          // removelast item
shift();        //remove first item
unshift();   // add item to begning
toString();   // convert to string
slice();      // extract part of string
push();    //add item to end of array



Looping through array

if you want to add data to an array of a company which have large data inside or you check if it is already added then you need array looping.  here is an example:


var company=["flipkart", "amazon","ebay"];
var newEntry="snapdeal";

for(var i=0; i<=company.length; i++)
{
 document.write("Length of an array before adding new Entry = "+ company.length);
      if (company[i] === newEntry){
        document.write(newEntry + " already exist"+ "<br>");
}
elseif(i===company.length-1)
{
             company.push(newEntry); 

document.write("Length of an array after adding new Entry = "+ company.length);
}
}

Comments