Home

Javascript Variables

To set variables in javascript you need to use the command
var or const (in lowercase)

eg... (for VARIABLES that you can change the values of)
   var x;
or    var x = 5;
or    var x = "ABCD";

or for setting multiple variables at once you can...
   var obj = {
      x:5, y:6,
      myAlph:"ABCD"
   };

or (for CONSTANTS that you can NOT change the values of)
   const x = 5;
   const x = "ABCD";
   const obj = {x:5, y:6, myAlph:"ABCD"};

or (for BOOLEANS you should set the value when declaring it)
   const myBool = True;
   var myBool = False;

Note...
You can also say
let myVar = "abc";
instead of var myVar = "abc" but let may not work everywhere.
Some people say LET should be used within functions but, for now, stick with var.

The proof...

The value of 'a' in:
  var a=5;

The value of 'z' in :
  var objM = {x:5, y:6}
  var z = (objM.x + objM.y);

var b=6;
b = b + 4.5;



ALSO Note...
When creating a variable you can also incorporate a 'truthy/falsey' way of thinking.
This is where, for example, if another variable is blank/empty or null it sets another value... by this I mean:-

var username = "Gary";
if (username.length==0) {
   myUser = "No Name";
} else {
   myUser = username;
}


Can also be sone by saying...

var username = "Gary";
var user = username || "No Name";


So if username is empty it gives the variable user the value of "No Name"
and if it has a value it gives user the value of username
(|| means 'or')

Javascript Operators

Examples of some of the operators used in Javascript...

Basic Operators:-
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder)
++ Increment Number by 1
-- Decrease Number by 1
= Set value to

You can also have
   +=, -=, *=, /=
Which would add/minus/multiply/divide a value to your variable
eg...Assume x = 10...
x += 5 will give 15 (10+5=5)
x -= 5 will give 5 (10-5=5)
x *= 4 will give 40 (10*4=40)
x /= 4 will give 2.5 (10/4=2.5)
(it's just less code to say x+=y rather than x=x+y)


Comparison Operators:-
== Equal value to (check value)
=== Equal value AND Equal Type
!= Not Equal to
!== Not Equal to OR not Equal Type
> Greater than
< Less than
>= Greater than or Equal to
<= Less than or Equal to
? Ternary operator

Logical Operators:-
&& Logical AND
|| Logical OR
! Logical NOT

The proof...

You would say: a = 5; to set a to equal 5

But to check if a value is equal to something you would use == (two equal signs)...eg...

if (a == 5) then {
//Code if True
} else {
//Code if FALSE
}


************

the '%' (modulus) is used to check for a REMAINDER value
and to also see if a value is ODD or EVEN.
eg...
25 % 2 would give a remainder of 1
24 % 2 would give a remainder of 0
...in fact ANY number DIVIDED BY 2 would give a remainder of 0 or 1
so to check if a value is
ODD - the remainder MUST be 1
EVEN - the remainder MUST be 0





************

The Ternary Operator is a conditionary operator.
It can set a value based on a condition.
(variable x = (expression) ? value if true: value if false)
...eg...assume a=10

...eg...assume a=1


or, instead of
if (a==1) {b=100} else {b=0};
you could say
a==1 ? b=100 : b=0;


************