change value of variable javascript code example
Example 1: javascript declare a variable
//choose the best for your solution
var myVariable = 22; //this can be a string or number. var is globally defined
let myVariable = 22; //this can be a string or number. let is block scoped
const myVariable = 22; //this can be a string or number. const is block scoped and can't be reassigned
Example 2: how to change a variables value in javascript
var variable = 10;
function start() {
variable = 20;
}
console.log(variable + 20);
// Answer will be 40 since the variable was changed in the function
Example 3: change a variable outside a function js
var global = "Global Variable"; //Define global variable outside of function
function setGlobal(){
global = "Hello World!";
};
setGlobal();
console.log(global); //This will print out "Hello World"
Example 4: variable in js
////We can define variables in 3 ways {var,let,const}///////
////syntax- var variable_name=value;
///// let variable_name=value;
/////const variable_name=value;
var myfristvariable="neha jaiswal";
const mysecondvariable=80;
let mythirdvar=abc;