// Converting a String into a Number in Javascript// Longhand:const num1 =parseInt("100");const num2 =parseFloat("100.01");console.log(num1);console.log(num2);// Shorthand:const num3 =+"100";// converts to int data typeconst num4 =+"100.01";// converts to float data typeconsole.log(num3);console.log(num4);
var x =parseInt("1000",10);// you want to use radix 10// so you get a decimal number even with a leading 0 and an old browser ([IE8, Firefox 20, Chrome 22 and older][1])
//string to number (typecasting)console.log(typeof("pp"))//Stringconsole.log(typeof(+"pp"))//Numberconsole.log((+"pp"))//Nanconsole.log(typeof(+""))//Numberconsole.log((+""))//0console.log(typeof(+"-6"))//Numberconsole.log((+"-6"))//-6// application of above conceptlet st=prompt("enter num")//Stringconsole.log(typeof(st))//${output entered in prompt box} let stnn=prompt("enter num")console.log(typeof(stnn))//Stringlet stnn =+stnn;//converting stnn variable as Numberconsole.log(typeof(stnn))//Numberconsole.log((stnn))//${output entered in prompt box}
//shorthand method for casting a string into an intlet string ='1776';let total =10-+string;//above the addition symbol does the same as parseInt(string);//result: total = 1766
$("#link").attr("href");// get an attribute$("#link").attr("href",'https://htmlg.com');// set attribute$("#link").attr({"href":"https://htmlg.com",// setting multiple attributes"title":"HTML Editor"});$("#link").attr("href",function(i, origValue){return origValue +"/help";// callback function gets and changes the attribute});