[Solved] How to set “p” tag data value to a number?


Javascript is an untyped language. The variable type is deduced from the value of that variable. The type of a variable could also change a value of another type is assigned to it. So things like this (for example C language):

int a; // a is an int (it will be always an int)
float f = 5.34; // f is a float (it will always be a float)

do not exist in Javascript. Javascript could use the same variable to store multiple types without redeclaring the variable.

var a = 45; // a type is deduced from the current assignement so for now it's a number
a = "string"; // now the type of a is a string not a number anymore 

You can explicitly or implicitly convert one type to another when needed.

Explicit Conversation:

you can convert a number into a string (although it will not be necessary) using .toString like this:

var num = 456;

console.log("num is of type: " + typeof(num));

var str = num.toString(); //explicitly change num to string

console.log("str is of type: " + typeof(str));

You can also convert a string into a number explicitly (this is used a lot) using parseInt if the string is an integer, parseFloat if the string is a float, or Number to get any like this:

var str="123.45.99";

console.log("str is of type: " + typeof(str));

var num1 = parseInt(str); // will parse an integer untill the first non integer character is found (will return 12 if str == "12ppp")

console.log("num1 is of type: " + typeof(num1));

var num2 = parseFloat(str); // parses a float untill the first non float character is found (will return 75.56 if str == "75.56.55abc"

console.log("num2 is of type: " + typeof(num2));

var num3 = Number(str); // get number representation of the string (will fail (return NaN) if the string is not a valid number like "123r33")

console.log("num3 is of type: " + typeof(num3));

Implicit Conversation:

Implicit conversation is when you let for the interpretter no other choice except of handling your variable as of your type. Thus preventing it from interpretting them incorrectly. You can acheive this using a lot of ways.

To implicitly convert a number into a string, just add an empty string to that number like this:

var num = 345;

console.log("num is of type: " + typeof(num));

var str = "" + num;

console.log("str is of type: " + typeof(str));

To convert a string into a number, there are multiple ways like this:

var str="123.45";

console.log("str is of type: " + typeof(str));

var num1 = +str; // a unary operator (that is valid for numbers only) forces the interpretter to use the value of str as a number

console.log("num1 is of type: " + typeof(num1));

Since the + operator (not the unray one) could be used for both concatenation and the addition the interpretter will always favor the concatenation on the addition if one of the operands is a string. But the other operator (/, *, / and %) are only used for number, so when a string is divided by another number, the interpretter will be forced to use the value of the string as a number:

var str = "10";

var num1 = str + 5;

console.log("num1 is: " + num1 + " and it's of type: " + typeof(num1)); // print out the wrong expectation, because since one of the operands of the + is a string (str) the interpretter will think it's a concatenation


var num2 = str * 5; // since * could be used with just numbers, the interpretter is forced to use str as a number (implicitly converting str to a number)

console.log("num2 is: " + num2 + " and it's of type: " + typeof(num2));


// to make the + operator work you'll have to use the unary operator to implicitly convert str before adding the numbers

var num3 = +str + 5;

console.log("num3 is: " + num3 + " and it's of type: " + typeof(num3));

// ++ and -- are also used with just number ..

str++; // since str++ is str = something and that something is calculated as number str will implicitly take the value of a number thus when used again it will be deduced as a number


console.log("str is: " + str + " and it's of type: " + typeof(str)); // in the previous assignment, str took the value of a number thus becoming a number

0

solved How to set “p” tag data value to a number?