types of variables in javascript code example

Example 1: type of variable js

// get type of variable

var number = 1
var string = 'hello world'
var dict = {a: 1, b: 2, c: 3}

console.log(typeof number) // number
console.log(typeof string) // string
console.log(typeof dict)   // object

Example 2: js var

var a;
console.log(a);                // scrive in console "undefined" o "" a seconda del browser usato.
console.log('still going...'); // scrive in console "still going...".

Example 3: variables in js

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

Example 4: data types in javascript

//String Data Type
var strSingle = 'John'; //String with single quotes
var strDouble = "Bob"; //String with double quotes

//Number Data Type
var num = 25; //Integer
var flo = 80.5; //Floating-point number
var exp = 4.25e+6; //Exponential notation, this equates to 4250000

//Boolean Data Type
var isReading = true; //Yes, I'm reading
var isSleeping = false; //No, I'm not sleeping

//Undefined Data Type
var undef; //If a value is never assigned, any output will be 'undefined'

//Null Data Type
var noValue = null; //Null meaning that it is has no value, not the same as 0 or ""

//Object Data Type
var emptyObject = {};
var person = {"name": "Clark", "surname": "Kent", "age": "36"}; //The quotes around the propety name can be omitted if the property name is a valid JS name
var car = { //Same as person but easier to read
	model: "BMW X3", //Example with quotes around property name ommitted
	color: "white",
	doors: 5
}

//Array Data Type
var emptyArray = []; //An array can be of any data types (string, number, boolean, etc.)
var array = ["One", "Two"] //String array, note the index of the first element is 0

//Function Data Type
var func = function() { //Calling the function: func();
  alert("Code excuted"); //Outputs: Code executed
}

var funcVar = function(amount) { //Calling the function: funcVar(6); 
  alert("Code excuted " + amount + " times"); //Outputs: Code executed 6 times (if input was 6)
}

//Typeof Operator
typeof variable; //Returns the data type of the variable

Example 5: js data types

//There are 7 data types in JS 
//They're split in two categories - (Primitives and Complex Types)

//Primives
string, number, boolean, null, undefined

//Complex types
Object, Function

Example 6: how to declare variables javascript

//variables are a way to easily store data in javascript
//variables are declared by the var keyword, example...
var x = 10;
//or
var dog_name = "daisy";
//which can also be written as 
var dog_name = 'daisy';
//same thing with single quotes and double quotes