JavaScript supports several data types such as integers, floating-point numbers, booleans, and strings. These data types are used to store different kinds of values in a program. To store and manipulate data in JavaScript, variables are used. Variables are declared using the "var" keyword, and values can be assigned to them using the assignment operator. Variables must be given an identifier, which is a unique name that is used to refer to the variable throughout the program. By understanding data types and variables, JavaScript developers can create robust and dynamic programs.
var count = 5;
var count = 5; // variable holds an integer value
count = 'hello'; // the same variable now holds a string
var name = 'Teleirk Academy'; // variable holds a string
var mark = 5.25 // mark holds a floating-point number
var counter = 5;
var studentsCount = 5;
var maxInteger = 9007199254740992;
var minInteger = -9007199254740992;
var a = 5, b = 3;
var sum = a + b; // 8
var div = a / 0; // Infinity
The floating-point type can hold numbers from 5e-324 to 1.79e+308
var PI = Math.PI; // 3.141592653589793
var minValue = Number.MIN_VALUE; // 5e-324
var maxValue = Number.MAX_VALUE; // 1.79e+308
var div0 = PI / 0; // Infinity
var divMinus0 = -PI / 0; // -Infinity
var unknown = div0 / divMinus0; // NaN
var a = 0.1;
var b = 0.2;
var sum = 0.3;
var equal = (a+b == sum); // false!!!
console.log('a+b = '+ (a+b) + ', sum = ' +
sum + ', sum == a+b? is ' + equal);
var value = 5;
value = 3.14159;
value = new Number(100); // Number { 100 }
value = value + 1; // 101
var biggestNum = Number.MAX_VALUE;
var valueDouble = 8.75;
var valueInt = valueDouble | 0; // 8
var valueDouble = 8.75;
var roundedInt = (valueDouble + 0.5) | 0; // 9
var str = '1234';
var i = str | 0 + 1; // 1235
Example of boolean variables taking values of true or false :
var a = 1;
var b = 2;
var greaterAB = (a > b);
console.log(greaterAB); // false
var equalA1 = (a == 1);
console.log(equalA1); // true
var s = 'Welcome to JavaScript';
var name = 'Nikolay' + ' ' + 'Kostov';
var firstName = 'Nikolay';
var lastName = 'Kostov';
console.log('Hello, ' + firstName + '!');
var fullName = firstName + ' ' + lastName;
console.log('Your full name is ' + fullName);
var asSalamuAlaykum = 'السلام عليكم';
alert(asSalamuAlaykum);
var кирилица = 'Това е на кирилица!';
alert(кирилица);
var leafJapanese = '葉';
alert(leafJapanese);
var numberString = '123'
console.log(parseInt(numberString); //prints 123
var floatString = '12.3';
console.log(parseFloat(floatString); //prints 12.3
var str = '123Hello';
console.log(parseInt(str)); //prints 123
'123.3' | 0 -> returns 123
Number('123.3') -> returns 123.3
'123.3' * 1 -> returns 123.3
+'123.3' -> returns 123.3
var x = 5;
x = undefined;
alert(x); // undefined
x = null;
alert(x); // null
The variable type can be checked at runtime:
var x = 5;
console.log(typeof(x)); // number
console.log(x); // 5
x = new Number(5);
console.log(typeof(x)); // object
console.log(x); // Number {}
x = null;
console.log(typeof(x)); // object
x = undefined;
console.log(typeof(x)); // undefined
var <identifier> [= <initialization>];
var height = 200;
Examples of correct identifiers:
var New = 2; // Here N is capital
var _2Pac; // This identifier begins with _
var поздрав = 'Hello'; // Unicode symbols used
// The following is more appropriate:
var greeting = 'Hello';
var n = 100; // Undescriptive
var numberOfClients = 100; // Descriptive
// Overdescriptive identifier:
var numberOfPrivateClientOfTheFirm = 100;
Examples of incorrect identifiers:
var new; // new is a keyword
var 2Pac; // Cannot begin with a digit
Assigning values example:
var firstValue = 5;
var secondValue;
var thirdValue;
// Using an already declared variable:
secondValue = firstValue;
// The following cascade calling assigns
// 3 to firstValue and then firstValue
// to thirdValue, so both variables have
// the value 3 as a result:
thirdValue = firstValue = 3; // Avoid this!
Example of some initializations:
// This is how we use a literal expression:
var heightInMeters = 1.74;
// Here we use an already initialized variable:
var greeting = 'Hello World!';
var message = greeting;
var a = 5; // a is local in the current scope
a = 'alabala'; // the same a is referenced here
a = undefined;
a = 5; // the same as window.a = 5;