how to add variables in 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: add variables in javascript

var a = 5;
var b = 2;
var c = a + b;

Example 3: adding numbers in Javscript

return a+b;

Example 4: how to add a variable in js

var x = 5;         // assign the value 5 to x

Example 5: how to add a variable in js

var x = 5;         // assign the value 5 to x

Example 6: how to assign variables in javascript

// data from json: resp = {"home": 1, "landmark": 2}
// Method 1: 
// use ` for add variable
url = `workspace/detail/${resp.home}`;
console.log(url) -> // workspace/detail/1
  
// Method 2: 
// use ' and + for concentrace variable
url = 'workspace/detail/' + resp.home;
console.log(url) -> // workspace/detail/1

Tags:

Html Example