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;
- JavaScript Exception handling
try { ShowMessage("This function is not defined anywhere so it will go to catch statement and execute the statements of catch block."); } catch(err) { document.getElementById("sample").innerHTML = err.message; }
- JavaScript do while statement
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!");
- JavaScript label statement
var a, b; outer: for (a = 0; a < 2; a++) { inner: for (b = 0;b < 2; b++) { if (a === 1 && b === 1) { continue outer; } document.write("a = " + a + ", b = " + b + " "); } }
- JavaScript break statement
- JavaScript continue statement
- JavaScript for in statement
- JavaScript function
- JavaScript Assignment operators
- JavaScript Comparison operators
- Javascript Arithmetic operators
- JavaScript Bitwise operators
- JavaScript Logical operators
- JavaScript String operators
- JavaScript Conditional operators
function voteableAge() { var age, voteable; age = document.getElementById("age").value; voteable = (age < 18) ? "Too young":"Old enough"; document.getElementById("sample").innerHTML = voteable + " to vote."; } </
- JavaScript typeof operator
function operatorFunction() {
var val=5; alert(typeof val);
}- JavaScript void operator
Click the following, This won't react at all...
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; }
- JavaScript lastIndexOf function
- JavaScript localeCompare function
- JavaScript match function
function lastOccurance() { var str = "I like learnkode and i love learnkode."; var n = str.lastIndexOf("learnkode"); 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; }
- JavaScript search function
- Javascript slice function
var vegs = ["Tomato", "Patato", "Lemon", "Cheese", "Carrot"]; var result= vegs.slice(1, 4); document.write(result);
- JavaScript split function
function splitString() { var str = "I Love LearnKode"; var result = str.split(" "); document.getElementById("sample").innerHTML = result; }
- JavaScript substr function
function extractSubpart() { var str = "I Love LearnKode."; var result = str.substr(1, 5); document.getElementById("sample").innerHTML=result; }
- JavaScript substring function
function extractSubset() { var str = "i love learnkode"; var result = str.substring(7, 16); document.getElementById("sample").innerHTML = result; }
- JavaScript toLocaleLowerCase function
function convertToLowercase() { var str = "I LOVE LEARNKODE"; var result = str.toLocaleLowerCase(); document.getElementById("sample").innerHTML = result; }
- JavaScript toLocaleUpperCase function
- Javascript toString function
- JavaScript trim function
- JavaScript valueOf function
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; }
- JavaScript reverse function
var arr= ['abc', 'xyz', 'def']; arr.reverse(); document.write(arr);
No comments:
Post a Comment