JavaScript in Internal File
Like other programming languages, JavaScript also provides 2 different data types to hold different types of values. Here are the two types of data types:
- Primitive data type
- Non-primitive data type
JavaScript is a dynamic type language, which means the type of the variable doesn’t need to be specified (it is dynamically used by JavaScript engine).
The keyword var is used here to declare a variable.
The datatype is declared when it is initialized with a particular values such as numbers, strings etc. For example:
- var a=10000; //holding number
- var b=”JavaScript”; //holding string
- var c; // c is now undefined
- c= 25 // cis a Number
- c = “JavaScript” //c is string
JavaScript primitive data types
These are the five types of primitive data types in JavaScript:
Data Type | Description |
---|---|
String | String represents sequence of characters e.g. “JavaScript” |
Number | Number represents numeric values e.g. 50 |
Boolean | Boolean represents either false or true |
Undefined | Undefined represents value that can’t be defined like infinity etc |
Null | Null represents nothing i.e. no value |
JavaScript non-primitive data types
The three JavaScript non-primitive data types are as follows:
Data Type | Description |
---|---|
Object | It represents instance through which we can access members and methods |
Array | It represents collection of similar data type values |
JavaScript String Rules
Quotes can be used inside a string if they don’t match the quotessurrounding the string:
1 2 3 4 5 6 7 | <script> var result= "It’s a tutorial!" //single quote inside double quote var result= "It is called ‘JavaScript"; //single quotes inside double quote var result = 'It is called "JavaScript" '; //double quotes inside single quote </script> |
JavaScript evaluates expressions from left to right. Also it mconcatenates numbers with strings in an expression. Few examples are as follows:
1 2 | var x = 61 + 4 + "JavaScript"; //Output:65 JavaScript var x ="JavaScript " + 10 + 4; // Output : JavaScript 104(string concatenation) |
DifferenceBetween Undefined and Null
Undefined and null are equal with respect to value but different with respect to type.
The typeof null is object whereas undefined is not an object.
JavaScript Arrays
1 2 3 | null === undefined //Result: false null == undefined // Result: true |
JavaScript arrays are written with square brackets and the array items are separated by comma.
The following code creates an array called colours and initializes with three colours:
1 | colours =["Blue","Violet","Black"] |
Array index starts with zero, which means the first item is colours[0], second is colours[1], and so on.
JavaScript Objects
JavaScript objects are written with curly braces like the following example below:
1 | var address = {Houseno:"221B", Lane:"Baker Street ", country:"UK"}; |