Sunday, December 24, 2017

JavaScript Inbuilt Functions








EXAMPLE
  • JavaScript getDate

function getDay() {
    var date = new Date();
    var day = date.getDate();
    document.getElementById("sample").innerHTML = day;
}
function evaluateExpression() {
    var x = 20;
    var y = 30;
    var a = eval("x * y") + "
";
    var b = eval("2 + 2") + "
";
    var c = eval("x + 17") + "
";
    var d = eval("18 + y") + "
";
    var result = a + b + c +d;
    document.getElementById("sample").innerHTML = result;
}

  • Javascript isFinite function
  • function isFiniteNumber() {
        var a = isFinite(4) + "
    ";
        var b = isFinite(-4) + "
    ";
        var c = isFinite(4-2) + "
    ";
        var d = isFinite(0) + "
    ";
        var e = isFinite("145") + "
    ";
        var f = isFinite("LearnKode") + "
    ";
    
        var result = a + b + c + d + e + f ;
        document.getElementById("sample").innerHTML = result;
    }

  • Javascript isNaN functions
  • function checkNumber() {
        var a = isNaN(NaN) + "
    ";
        var b = isNaN(undefined) + "
    ";
        var c = isNaN(null) + "
    ";
        var d = isNaN(37) + "
    ";
         numbers=a+b+c+d;
         document.getElementById("sample").innerHTML = numbers;
    }

  • Javascript parseInt/Float functions
function convertToFloat() {
    var a = parseFloat("10") + "
";
    var b = parseFloat("10.00") + "
";
    var c = parseFloat("10.33") + "
";
    var d = parseFloat("34 45 66") + "
";
    var e = parseFloat("   60   ") + "
";
    var f = parseFloat("40 years") + "
";
    var g = parseFloat("I am 40") + "
";
    var result = a + b + c + d + e + f + g;
    document.getElementById("sample").innerHTML = result;
}

var person = {
    firstName : "Michael",
    lastName  : "Voughan",
    age       : 40,
    eyeColor  : "blue"
};
document.getElementById("sample").innerHTML =
person.firstName + " " + person.lastName;


  • Javascript Number
  • function convertToNumber() {
        var a = false;
        var b = true;
        var c = new Date();
        var d = "888";
        var e= "666 555";
        var result = 
        Number(a) + "
    " + 
        Number(b) + "
    " + 
        Number(c) + "
    " + 
        Number(d) + "
    " + 
        Number(e);
        document.getElementById("sample").innerHTML = result;
    }

  • JavaScript Date object
  • var date = new Date("December 13, 2015 11:13:00");

  • JavaScript Math Object
  • var result = Math.sqrt(16);
    document.getElementById("sample").innerHTML = result;

  • JavaScript Array
  • var vehicles= ["Alto", "Swift", "BMW"];
    document.getElementById("sample").innerHTML = vehicles;

  • Javascript Comments
  • // commented statements
    document.getElementById("mydiv").innerHTML = "My First Page";
    /*
    document.getElementById("mydiv").innerHTML = "My first paragraph.";
    */
  • JavaScript Data types
  • JavaScript special characters
  • JavaScript if condition
  • function dayGreeting() {
        var age = 19 
        var message;
        if (age < 18) {
            message = "You are not qualified for Voting";
        } else {
            message = "You are qualified for Voting";
        }
        document.getElementById("sample").innerHTML = message;
    }
  • Javascript For Loop
   var count;
    document.write("Starting Loop" + "
");      
    for(count = 0; count < 10; count++){
    document.write("I am : " + count );
    document.write("
");
    }

    document.write("Loop stopped!");
            document.write("Starting Loop ");
         
            while (count < 10){
               document.write("I am : " + count + "
");
               count++;
            }
         
            document.write("Loop stopped!");
        
var grade = 'A';
var message = '';
switch (grade) {
    case 'A':
        message = "Excellent";
        break;
    case 'B':
        message = "Good";
        break;
    case 'C':
        message = "OK";
        break;
    case 'D':
        message = "Mmmmm....";
        break;
    case 'E':
        message = "You must do better than this";
        break;
    default: 
        message = "What is your grade anyway?";
        break;
}
document.getElementById("sample").innerHTML =  message;

var count = 0;
document.write("Here is the start of loop: " + "
");

do{
 document.write(" Counter : " + count + "
");
 count++;
} while (count < 10);

document.write ("End of loop!");
href="javascript:void(alert('Warning!!!'))">Click me!



  • JavaScript toExponential functional
  • JavaScript toFixed function

  • function convertToFixed() {
        var num = 6.76549;
        var exp = num.toFixed(3);
        document.getElementById("sample").innerHTML = exp;
    }

    function joinArrays() {
        var cars = ["Alto", "Zen"];
        var buses = ["Punbus", "PePso", "Linus"];
        var result = cars.concat(buses); 
        document.getElementById("sample").innerHTML = result;
    }
    function findPosition() {
        var str = "Hello world, welcome to learnKode.";
        var n = str.indexOf("welcome");
        document.getElementById("sample").innerHTML = n;
    }

    function matchString() {
        var str = "I love learnKode that keeps my learning of code so easy."; 
        var result = str.match(/earn/g);
        document.getElementById("sample").innerHTML = result;
    }

    • JavaScript replace function
    • function newString() {
          var str = document.getElementById("sample").innerHTML; 
          var result = str.replace("Google", "LearnKode");
          document.getElementById("sample").innerHTML = result;
      }


    function convertToPrimitiveValue() {
        var str = "I love learnKode";
        var result = str.valueOf();
        document.getElementById("sample").innerHTML = result;
    }

    • JavaScript pop function
    var fishName= ['angel', 'clown', 'mandarin', 'sturgeon'];
    document.getElementById("sample").innerHTML = fishName;
    
    function removeElement() {
        fishName.pop();
        document.getElementById("sample").innerHTML = fishName;
    }
    • JavaScript push function
    • Adding elements at the end
    • var arr= ['a', 'b','c','d'];
      document.getElementById("sample").innerHTML = arr;
      function addElement() {
          arr.push("e","f");
          document.getElementById("sample").innerHTML = arr;
      }
    • JavaScript shift function
    Removes first element of array

    document.getElementById("sample").innerHTML = arr;
    
    function removeElement() {
        arr.shift();
        document.getElementById("sample").innerHTML = arr;
    }
    • Adds 0 as first element of array
    • document.getElementById("sample").innerHTML = arr ;
      
      function addElement() {
          arr.unshift(0);
          document.getElementById("sample").innerHTML = arr ;
      }
    • JavaScript sort function

    var arr= ['ABC', 'XYX', 'BCD'];
    document.getElementById("sample").innerHTML = arr;
    
    function sortArray() {
        fruits.sort();
        document.getElementById("sample").innerHTML = arr;
    }

    No comments:

    Post a Comment