<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> var str = "Hello"; var arr = str.split(""); var text = ""; var i; for (i = 0; i < arr.length; i++) { text += arr[i] + "<br>" } document.getElementById("demo").innerHTML = text; </script> </body> </html>
Js Infinity Code:
<!DOCTYPE html> <html> <body> <h2>JavaScript String Methods</h2> <p id="demo"></p> <script> var myNumber = 2; var txt = ""; while (myNumber != Infinity) { myNumber = myNumber * myNumber; txt = txt + myNumber + "<br>"; } document.getElementById("demo").innerHTML = txt; </script> </body> </html>
<!DOCTYPE html> <html> <body> <h2>JavaScript String Methods</h2> <p id="demo"></p> <script> var x=2/0; var y=-2/0; document.getElementById("demo").innerHTML=x+"<br>"+y; </script> </body> </html>
<!DOCTYPE html> <html> <body> <h2>JavaScript String Methods</h2> <p id="demo"></p> <script> var x=Infinity; document.getElementById("demo").innerHTML=typeof x; </script> </body> </html>
toString() methods in JavaScript
<!DOCTYPE html> <html> <body> <h2>JavaScript String Methods</h2> <p id="demo"></p> <script> var myNumber=32;//x will be 255 document.getElementById("demo").innerHTML="32="+"<br>"+ "Decimal "+myNumber.toString(10)+"<br>"+ "hexadecimal "+myNumber.toString(16)+"<br>"+"Octal "+myNumber.toString(8)+"<br>"+"Binary "+myNumber.toString(2); </script> </body> </html>
toString() methd also convert number to string
<!DOCTYPE html> <html> <body> <h2>JavaScript Number Methods</h2> <p>The toString() method converts a number to a string.</p> <p id="demo"></p> <script> var x = 123; document.getElementById("demo").innerHTML = x.toString() + "<br>" + (123).toString() + "<br>" + (100 + 23).toString(); </script> </body> </html>
var x=9.656;
x.toExponential(2); //returns 9.66+0
x.toExponential(4); //returns 9.6560e+0
x.toExponential(6);//returns 9.656000e+0
var x=9.656;
x.toFixed(0); //returns 10
x.toFixed((4); //returns 9.66
x.toFixed(6); //returns 9.656000
var x=9.656
MAX_VALUE
MIN_VALUE
POSITIVE_INFINITY
NEGATIVE_INFINITY
NaN=”Not a Number” value
<!DOCTYPE html> <html> <body> <h2>JavaScript String Methods</h2> <p id="demo"></p> <script> var fruits, fLen, i, text; fruits = ["Banana", "orange", "Apple", "mango"]; fLen = fruits.length; text = "<ul>"; for (i = 0; i < fLen; i++) { text +="<li>"+ fruits[i] + "</li>"; } text += "</ul>"; document.getElementById("demo").innerHTML = text; </script> </body> </html>
<!DOCTYPE html> <html> <body> <h2>JavaScript String Methods</h2> <button onclick="myFunction()"> Click Here To Add New Element TO Array </button> <p id="demo"></p> <script> var fruits = ["Banana", "orange", "Apple", "mango"]; document.getElementById("demo").innerHTML = fruits; function myFunction() { fruits.push("Lemon"); document.getElementById("demo").innerHTML = fruits; } </script> </body> </html>
<!DOCTYPE html> <html> <body> <h2>JavaScript String Methods</h2> <p id="demo"></p> <script> var fruits = ["Banana", "orange", "Apple", "Mango"]; fruits[6]="Lemon"; // document.getElementById("demo").innerHTML = fruits; fLen=fruits.length; text=""; for(i=0;i<fLen;i++){ text+=fruits[i]+"<br>"; } document.getElementById("demo").innerHTML = text; </script> </body> </html>
Output:
JavaScript String Methods Banana orange Apple Mango undefined undefined Lemon
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <p id="demo1"></p> <p id="demo2"></p> <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits; fruits.pop(); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
//Sort the elements of the array alphabetically
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <p id="demo1"></p> <p id="demo2"></p> <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits; fruits.sort(); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <p id="demo1"></p> <p id="demo2"></p> <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits; fruits.sort(); fruits.reverse(); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Js Run in Console and Array Sort;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
var stringsArray=['Blue','Humpback','Beluga']; var numericStringArray=['80','9','700']; var numberArray=[40,1,5,200]; var mixedNumericArray=['80','9','700',40,1,5,200]; function compareNumbers(a,b) { return a-b; } console.log("StringArraay: ",stringsArray.join()); console.log("StringArraay: ",stringsArray.sort()); console.log("StringArraay: ",numberArray.join()); console.log("StringArraay: ",numberArray.sort(compareNumbers)); console.log("StringArraay: ",mixedNumericArray.sort(compareNumbers));
JavaScript learning also good site:
https://javascript.info/
https://www.w3schools.com/js
https://guide.freecodecamp.org/javascript/additional-javascript-resources