// 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])
// parseInt is one of those things that you say wtf javascript?//if you pass to it any argument with number first and letters after, it// will pass with the first numbers and say yeah this a number wtf?let myConvertNumber =parseInt('12wtfwtfwtf');console.log(myConvertNumber);// the result is 12 and no error is throw or something//but thislet myConvertNumber =parseInt('wtf12wtf');console.log(myConvertNumber);// is NaN wtf?//if you truly want an strict way to know if something is really a real number// use Number() insteadlet myConvertNumber =Number('12wtf');console.log(myConvertNumber);// with this if the string has any text the result will be NaN
//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});