Data Types of Javascript— JS wall
JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript.
- Primitive data type
- Non-primitive (reference) data type
The basic difference between these two data types is based on how they store in memory.
Primitive Data Types:
String — represents sequence of characters e.g. “ethicals”
Number —represents numeric values e.g. 100
Boolean — represents boolean value either false or true
Null — represents null i.e. no value at all
Undefined — represents undefined value
Symbol — This primitive type is useful for so-called “private” or “unique” keys.
Non-primitive (reference) Data Type:
Array — represents group of similar values
Object — represents instance through which we can access members
RegExp — represents regular expression
String Functions —
Any value which is written either inside double quotes or single quotes is considered as String in Javascript. Use escape sequence to quotes visible in the output. //console.log(‘I love \’Company\’ launch’)
text.toUpperCase() — to convert everything to uppercase.
text.toLowerCase() — to convert everything to lowercase.
text1.concat(text2) — to add text2 string to text1
str.trim() — to remove all the extra space(in start and in end)
str.charAt(0) — to get the character for specific location.
txt.split(“,”) — split on commas
txt.split(‘ ’) — split on spaces
str.length — length of the string
str.indexOf(substr) — returns 1st appearance of substr in str and return-1 if found nothing and it is case sensitive
str.indexOf(substr,fromPos) — returns 1st appearance of substr after the given position.
str.lastIndexOf(substring) — searches for the last occurrence
str.slice(start, end) — It extracts parts of a string and returns the extracted parts in a new string. start-The position where to begin the extraction. end-The position (up to, but not including) where to end the extraction.
str.substr(start, length) — substr extracts length characters from a str, counting from the start index.
Number Functions —
There are some functions that are used to convert Number into String and Strings Numerals to numbers
variable.toString() — Is used to convert variable into string
parseInt(val) — It will convert string numerals into number but returns integer part only
parseFloat(val) — It will convert string numerals into number but returns both integer and decimal parts
Round off Numbers
variable.toFixed() — It will convert number into string numerals but remove the decimal parts
variable.toFixed(2) — It will convert number into string numerals but give the decimal value upto 2 places.
Null and Undefined —
Both are used to tell that there is no value. In JavaScript, undefined is a type, whereas null an object.
Undefined means a variable has been declared but has not been initialized or no value is assigned to it.
It means a variable declared, but no value has been assigned a value.
Null is a value assigned to a variable, maybe just to represent that variable holds no value. Whereas, null in JavaScript is an assignment value. You can assign it to a variable.
When javascript tells us there is no value, it uses undefined while when we have to write there is no value we use Null.
typeof()
It is used to check the data types of any value. ‘typeof()’ returns the string and tells the type of data inside it.
typeof(10) — // ‘number’, typeof(‘ethicals’) — // ‘string’
In the upcoming posts, we will be using all of these different data types in order to create websites and web apps.