To Print Javascript Variables value to JSX Elements tut 3

# To Print Javascript Variables value to JSX Elements


var fname = "Vrushant"
var lname = "Domde"
var age = 18

var currDate  = new Date().toLocaleDateString();
var currTime  = new Date().toLocaleTimeString();

ReactDom.render(
    <>
  <h1 contentEditable="true">Hello World</h1>
  <p>My name is {fname} </p>
  <p>{`My Full Name is ${fname} ${lname}`}</p>
  <p>My age is {age}</p>
  <p>RAndom Number {Math.random()}</p>
  <p> 5 + 10 is {5+10}</p>
   <p> Current Date : {currDate}</p>
   <p> Current Time : {currTime} </p>    </> ,
   document.getElementById('root')
);

Explanation :

  •    We  can print the value of variable "fname" in p tag by using { } bracket . We have to put variable name inside {} like { fname } , so that it will print value of fname variable.
  •     If we have to print  lots of variable  in same text  with some spacing , so we can use <p>{`My Full Name is ${fname} ${lname}`}</p>
  • Attribute contentEditable="true" enable user to edit the content in h1  like tag.
  • Math.random() function use to print random number

Comments

Popular posts from this blog

Remote Method Invokation (RMI) in Java

Changing the Title of Website on Button Click in React Hook tut 14

useContext() in React tut 12