Variables and Statements
I have an easy way of declaring constants and variables (integer, string, arrays etc). Why?
- I am dynamically typed language (means you don't have to worry about the type of variable like integer, string etc. I will do it for you at runtime).
- I provide 3 keywords const, var, let to declare variables.
1- var (MDN)
//This is how you can write a comment
var myNameInString = "JavaScript"; //this is string
var myNameInArray = ['J','a','v','a','S','c','r','i','p','t']; //this is array
var myBirthdayYear = 1995; // this is number
// below is an object
var myDetails = {
name: "JavaScript",
creator: "Brendan Eich"
}
console.log(myNameInString); //JavaScript
//console.log() is use to print the result of an expression
myNameInString = "My name is Javascript";
console.log(myName); //My name is Javascript
As I previously mentioned that I am dynamically typed so you can use only var to declare all type of variables. Try it on JSBin
2- const (MDN)
const myName = "JavaScript"
console.log(myName); //JavaScript
myName = "My Name is JavaScript"; // this will throw error
const is like constant. Once you declare and define, then you can't change the value of that constant (myName in our case) thereafter.
3- let (MDN)
let myName = "JavaScript"
console.log(myName); //JavaScript
myName = "My Name is JavaScript"; // My Name is JavaScript
let works similar to var but let is block scoped. (block-scoped is an advanced feature and you can read on MDN).
Note: Just use const for constants and let for variables instead of var.