{"id":8353,"date":"2022-09-13T04:05:16","date_gmt":"2022-09-12T22:35:16","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/"},"modified":"2022-09-13T04:05:16","modified_gmt":"2022-09-12T22:35:16","slug":"solved-how-to-set-p-tag-data-value-to-a-number","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/","title":{"rendered":"[Solved] How to set &#8220;p&#8221; tag data value to a number?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-42070815\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"42070815\" data-parentid=\"42070442\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Javascript is an <strong>untyped<\/strong> 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):<\/p>\n<pre><code>int a; \/\/ a is an int (it will be always an int)\nfloat f = 5.34; \/\/ f is a float (it will always be a float)\n<\/code><\/pre>\n<p>do not exist in Javascript. Javascript could use the same variable to store multiple types without redeclaring the variable.<\/p>\n<pre><code>var a = 45; \/\/ a type is deduced from the current assignement so for now it's a number\na = \"string\"; \/\/ now the type of a is a string not a number anymore \n<\/code><\/pre>\n<p>You can <strong>explicitly<\/strong> or <strong>implicitly<\/strong> convert one type to another when needed.<\/p>\n<h3>Explicit Conversation:<\/h3>\n<p>you can convert a number into a string (although it will not be necessary) using <code>.toString<\/code> like this:<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>var num = 456;\n\nconsole.log(\"num is of type: \" + typeof(num));\n\nvar str = num.toString(); \/\/explicitly change num to string\n\nconsole.log(\"str is of type: \" + typeof(str));<\/code><\/pre>\n<\/div>\n<\/div>\n<p>You can also convert a string into a number explicitly (this is used <strong>a lot<\/strong>) using <code>parseInt<\/code> if the string is an integer, <code>parseFloat<\/code> if the string is a float, or <code>Number<\/code> to get any like this:<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>var str=\"123.45.99\";\n\nconsole.log(\"str is of type: \" + typeof(str));\n\nvar num1 = parseInt(str); \/\/ will parse an integer untill the first non integer character is found (will return 12 if str == \"12ppp\")\n\nconsole.log(\"num1 is of type: \" + typeof(num1));\n\nvar num2 = parseFloat(str); \/\/ parses a float untill the first non float character is found (will return 75.56 if str == \"75.56.55abc\"\n\nconsole.log(\"num2 is of type: \" + typeof(num2));\n\nvar num3 = Number(str); \/\/ get number representation of the string (will fail (return NaN) if the string is not a valid number like \"123r33\")\n\nconsole.log(\"num3 is of type: \" + typeof(num3));<\/code><\/pre>\n<\/div>\n<\/div>\n<h3>Implicit Conversation:<\/h3>\n<p>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.<\/p>\n<p>To implicitly convert a number into a string, just add an empty string to that number like this:<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>var num = 345;\n\nconsole.log(\"num is of type: \" + typeof(num));\n\nvar str = \"\" + num;\n\nconsole.log(\"str is of type: \" + typeof(str));<\/code><\/pre>\n<\/div>\n<\/div>\n<p>To convert a string into a number, there are multiple ways like this:<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>var str=\"123.45\";\n\nconsole.log(\"str is of type: \" + typeof(str));\n\nvar num1 = +str; \/\/ a unary operator (that is valid for numbers only) forces the interpretter to use the value of str as a number\n\nconsole.log(\"num1 is of type: \" + typeof(num1));<\/code><\/pre>\n<\/div>\n<\/div>\n<p>Since the <code>+<\/code> 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 (<code>\/<\/code>, <code>*<\/code>, <code>\/<\/code> and <code>%<\/code>) 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:<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>var str = \"10\";\n\nvar num1 = str + 5;\n\nconsole.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\n\n\nvar 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)\n\nconsole.log(\"num2 is: \" + num2 + \" and it's of type: \" + typeof(num2));\n\n\n\/\/ to make the + operator work you'll have to use the unary operator to implicitly convert str before adding the numbers\n\nvar num3 = +str + 5;\n\nconsole.log(\"num3 is: \" + num3 + \" and it's of type: \" + typeof(num3));\n\n\/\/ ++ and -- are also used with just number ..\n\nstr++; \/\/ 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\n\n\nconsole.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<\/code><\/pre>\n<\/div>\n<\/div><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to set &#8220;p&#8221; tag data value to a number? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] 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) &#8230; <a title=\"[Solved] How to set &#8220;p&#8221; tag data value to a number?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/\" aria-label=\"More on [Solved] How to set &#8220;p&#8221; tag data value to a number?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[346,333],"class_list":["post-8353","post","type-post","status-publish","format-standard","hentry","category-solved","tag-html","tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to set &quot;p&quot; tag data value to a number? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to set &quot;p&quot; tag data value to a number? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] 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) ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-12T22:35:16+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to set &#8220;p&#8221; tag data value to a number?\",\"datePublished\":\"2022-09-12T22:35:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/\"},\"wordCount\":287,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"html\",\"javascript\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/\",\"name\":\"[Solved] How to set \\\"p\\\" tag data value to a number? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-12T22:35:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to set &#8220;p&#8221; tag data value to a number?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to set \"p\" tag data value to a number? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to set \"p\" tag data value to a number? - JassWeb","og_description":"[ad_1] 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) ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/","og_site_name":"JassWeb","article_published_time":"2022-09-12T22:35:16+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to set &#8220;p&#8221; tag data value to a number?","datePublished":"2022-09-12T22:35:16+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/"},"wordCount":287,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["html","javascript"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/","name":"[Solved] How to set \"p\" tag data value to a number? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-12T22:35:16+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-set-p-tag-data-value-to-a-number\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to set &#8220;p&#8221; tag data value to a number?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/8353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=8353"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/8353\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=8353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=8353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=8353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}