Description

In JavaScript, there can be two types of values, fixed and variable.

  • Fixed values are called Literals, which are constant in nature.
  • Variable values are called Variables, their values vary during the execution.

Literals

Literals are constant in nature and their values won't change during the program execution.

  • String literals must be enclosed with single or double-quotes.
  • Number literals must not be enclosed with quotes.
var name = "Jack Sparrow";    //String literal "Cheese" is enclosed with double quotes.
var address = '123 ABC STREET';    //String literal "address" is enclosed with single quotes.

var id = 1001;    //Number literal "1001" is not enclosed with quotes.
var price = 120.25;    //Decimal literal "120.25" is not enclosed with quotes.

Variables

Variables are the containers of values and their values vary during the program execution.

  • Variables must be defined using the keywords "var", "let", or "const".
  • Variables values may change during the program execution using expressions.
var name = "Jack Sparrow";    //Variable "name" get a value "Jack Sparrow" using the keyword "var" at this point.
let address = '123 ABC STREET';    //Variable "address" gets a value "123 ABS STREET" using the keyword "let" at this point.
const id = 1001;    //Variable "id" gets a value "1001" using the keyword "const" at this point.

console.log(name);    //Logs "Jack Sparrow" to browser console.

name = "John Cena";    //Variable "name" contains a value "John Cena" at this point.
console.log(name);    //Logs "John Cena" to browser console.

Overall

JavaScript syntax is nothing but a set of rules recommended for clean and maintainable code.

Related Links