Data Type

The primary data types of JavaScript are letters, numbers, and boolean. When declaring a variable, prefix the variable name with var.

var city = "New York";//or 'New York'
var result = 1200 / 1000;
var onGlass = true;
alert('city: ' + city);
alert('result: ' + result);
alert('onGlass: ' + onGlass);

Local variables and global variables

A local variable is a variable declared within a function, and you can only use it within the function. A global variable is a variable declared outside of functions, and you can use it anywhere. Variables declared without the var keyword are global, regardless of whether they are inside or outside a function. However, it is a good practice always to add var keywords when declaring variables.


Live variables, there are global constants and local constants. To declare a constant, prefix the constant name with const. It is better to use only uppercase letters and underscores (_) in constant names.

const PI = 3.14;
alert('PI: ' + PI);


// is a one-line comment. /* ~ */ is a multiline comment.


You can combine letters, numbers, _, and $ to create an identifier, which must not have the same name as any keyword. JavaScript identifies the case of identifiers.


The undefined means 'no value assigned.' You get this when you refer to an uninitialized variable.

var i;
alert('i: ' + i);

Returns false if condition is 0, "", NaN, undefined.

var i;
if (i) {
  alert('i: ' + "true");
} else {
  alert('i: ' + "false");