0
Q:

js variables

//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
16
var Number = 5;
var String = "Hi!";
var boolen1 = true;
var boolen2 = false;
var array = [11, "Hi!", true];
var object = {age:11, speach:"Hi!", likes_Bananas:true};
3
//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!!!
1

 var text = "";
var i;
for (i = 0; i < 5; i++) {
  text += "The number is " + i + "<br>";
} 
1
var variable1 = "some variable content";
1
//Assigns the value bar to the variable foo
var foo = bar;
1
	Variables can have anything, you define them as equal to something
    
    eg:   var x = 22
    var statement = 'hello!'
    var isAbool = true 
    
    there are three types of variables:
1. var 
2. let
3. const 

var is a normal variable
let is a variable whose value can be changed eg: let h = 12, h = 10(we changed the value, if you run this, there will be no error)
const is a variable whose value cannot be changed, if you change the value of a const, you will have errors in you code
0
// write what ever you want
// one way is
// this is to declare intigers
var a = 10;
//this is to declare strings aka words
var b = "hi";
// the key word var can be replaced by let
//like this
let a = 10;
let b = "hi";
// i recomend using let insted of var
4
// let & var are both editable variables:
var cow = 'moo';
let pig = 'oink';
cow = 10;
pig = 10;
// const is a permanent variable; you cannot change it.
const uncuttableGrass = '\/\/';
uncuttableGrass = '___';
// above throws an error
0
// Also let, const
var e_number = 0;
var e_string = '';
var e_true = true;
var e_false = false;
var e_undefined = undefined;
0

New to Communities?

Join the community