string variable in javascript code example

Example 1: how to declare a variable js

//let and var are both editable variables and can be changed later on in your program;
let dog = 'Woof';
//dog is equal to the string 'Woof';
dog = false;
//You can changed the value of dog now because it was defined with let and not const;

let cow = 'Moo';
//cow is equal to the string 'Moo';
cow = true;
//You can change the value of cow later on because it is not defined with const;

//const is used when declaring a variable that can't be changed later on -- const stands for constant;
const pig = 'oink';
//This assigns the string 'oink' to pig which can not be changed because it is defined with const;
pig = 'snort';
//Above throws an error
//Good Job you now know how to declare variables using JavaScript!!!

Example 2: Create variable javascript

//Assigns the value bar to the variable foo
var foo = bar;

Example 3: js var in string

// in your string you can call your variable like ${myVar}. eg:
var myVar = "blue";
console.log("The sky is " + myVar + ".");
// Output: "The sky is blue."

Example 4: how to use string variable in javascript

<html>
   <head>
      <title>JavaScript Strings</title>
   </head>
   <body>
      <script>
         var re = "java";
         var str = "Learn java";
         if ( str.search(re) == -1 ){
            document.write("Not found!" );
         } else {
            document.write("Found!" );
         }
     </script>
   </body>
</html>