Javascript is a programming language which vastly used in web scripting. In Javascript there so important things to discuss. But today we are gonna discuss 10 things which are important but don’t get that much.
There are six basic data types in JavaScript which can be divided into three main categories: primitive (or primary), composite (or reference), and special data types. String, Number, and Boolean are primitive data types. Object, Array, and Function (which are all types of objects) are composite data types. Whereas Undefined and Null are special data types.
Primitive data types can hold only one value at a time, whereas composite data types can hold collections of values and more complex entities. Let’s discuss each one of them in detail.
1) String:
The string data type is used to represent textual data. Strings are created using single or double quotes surrounding one or more characters, as shown below:
let a = 'Hi there!'; // using single quotes
let b = "Hi there!"; // using double quotes
2) Number:
The number data type is used to represent positive or negative numbers with or without decimal place or floating-point number.
let a = 25; // integer
let b = 80.5; // floating-point number
3) Boolean:
The Boolean data type can hold only two values: true
or false
. It is typically used to store values like yes (true
) or no (false
), on (true
) or off (false
), etc. as examples given below:
let isEating = true; // yes, I'm reading
let isWalking = false; // no, I'm not sleeping
3) Undefined:
The undefined data type can only have one value-the special value undefined. If a variable has been declared but has not been assigned a value, has the value undefined.
let a;
let b = "Hello World!"
console.log(a);
console.log(b);#Output:
undefined
"Hello World"
4) Null:
This is another special data type that can have only one value-the null
value. A null value means that there is no value.
let a = null;
console.log(a)
let b = "Hello World!"
console.log(b);
b = null;
console.log(b)#output:
null
"Hello World"
null
5) Object:
The object is a complex data type that allows you to store collections of data. An object contains properties, defined as a key-value pair. A property key (name) is always a string, but the value can be any data types, like strings, numbers, booleans, or complex data types like arrays, function and other objects. Simple Example given below:
let emptyObject = {};
let person = {name: "Aelex", surname: "Tomas", age: "26"};
let car = {
modal: "BMW X3",
"color": "Black",
"doors": 5
}
6) Array:
An array is a type of object used for storing multiple values in a single variable. Each value in an array has a numeric position, known as its index, and it may contain data of any data type-numbers, strings, booleans, functions, objects, and even other arrays. The array index starts from 0 so that the first array element is array[0].
let colors = ["Red", "Yellow", "Green", "Orange"];
let cities = ["Kuala Lampur", "Tokyo", "Istanbul"];
console.log(colors[0]);
console.log(cities[2])#output:
"Red"
"Istanbul"
7) Function:
A function is a callable object that executes a block of code. Since functions are objects, so it is possible to assign them to variables, as shown in the example below:
let greeting = function(){
return "Hello World!";
}
console.log(greeting())#output:
"Hello World"
8) The typeof() Operator:
The typeof operator can be used to find out what type of data a variable or operand contains. It can be used with or without parentheses (typeof(a) or typeof a).
typeof 15; // returns number
typeof 15.5 // returns numbertypeof("hello") // returns string
typeof('15') // returns stringtypeof(true) // returns boolean
typeof(false) // returns boolean
9) Error handling function ‘try catch’:
The try catch function is used for catching errors in code. If any code has bugs in it you can use the try catch function to detect the bug. If any error occurs the try cath function then provides details about the error in an error object where propertise are ‘name’, ‘message’, ‘stack’. An example is given below for better understanding:
try {
helllooo; // error, variable is not defined!
} catch (err) {
alert(err.name); // ReferenceError
alert(err.message); // helllooo is not defined
alert(err.stack); // ReferenceError: helllooo is not defined at (...call stack)
}
The try catch function can't cath any syntex in the code. the try catch function works at runtime if there is any error inside setTimeout function try catch won’t be able to catch the error.
10) Cross Browser Testing:
Cross-Browser testing is a type of non-functional testing that lets you check whether your website works as intended when accessed through Different Browser-OS combinations, Different devices.
Browser vendors follow Open Web Standards, but they have their own interpretations of it. Since they each render HTML, CSS, and JavaScript in unique ways, thoroughly debugging your website’s source code is not enough to ensure that your website will look and behave as intended on different browsers (or different versions of a single browser). So it's important to do Cross Browser Testing.