10 interesting JavaScript things !!
JavaScript is one of the prominent programming languages. It is used for web application developments as well as mobile application developments. Here, I present 10 features that I find interesting to know about.
String
- String Objects : The method String() creates an object out of a string it takes as input. Obviously, it has to be new !
const str_0 = “lorem ipsum”
const str_1 = new String( str_0 )
console.log( str_1 )
//String {“lorem ipsum”}
0: “l”
1: “o”
2: “r”
3: “e”
4: “m”
5: “ “
6: “i”
7: “p”
8: “s”
9: “u”
10: “m”
length: 11
2. String uppercase — lowercase : The method .toUpperCase() converts a string into all Uppercase letters, whereas .toLowerCase() converts a string into all lowercase letters.
const str_0 = “lorem ipsum”
const str_1 = str_0.toUpperCase( )
console.log( str_1 )
//”LOREM IPSUM”
const str_2 = str_1.toLowerCase( )
console.log( str_2 )
//”lorem ipsum”
Numbers
3. Integer Range for Number: Number in JavaScript has its limitations. There is a range of maximum and minimum numbers that a number object can hold safely. The range is as such -
const biggestInteger = Number.MAX_SAFE_INTEGER
console.log( biggestInteger)
//9007199254740991
const smallestInteger = Number.MIN_SAFE_INTEGER
console.log( smallestInteger )
//-9007199254740991
4. NaN : Not a Number. Very annoying! When a Number object is used as a function to convert numerical strings into numbers, then it may return NaN if the conversion is not possible. That is, the spring might not be numerical at all.
Number(‘1’) // returns the number 1
Number(‘10’) === 10 // returns true
Number(“lorem ipsum”) // NaN
Array
5. Joining Item to an array: The method .push( ) does that for you.
const flowers = [‘rose’, ‘sunflower’]
flowers.push( ‘tulip’)
console.log( flowers )
// [‘rose’, ‘sunflower’, ‘tulip’]
6. Copying an array: The method .slice( ) copies an array.
const flowers = [‘rose’, ‘sunflower’]
const flowers_copied = flowers.slice( )
console.log( flowers_copied)
//[‘rose’, ‘sunflower’]
Boolean
7. Primitive Boolean Values vs Boolean Object: Confusion may arise between Primitive boolean values and boolean objects, but they are not the same actually.
Primitive boolean values: true, false
var condition = false;
if(condition){
// does not execute. It gets false!
}
Boolean object: Boolean{false}. Or Boolean{true}
var condition = new Boolean(false) // Boolean{false}. This is an object and so the if statement finds true.
if(condition){
//does execute. Gets true.
}
8. Boolean values :
false: 0, -0, null, NaN, undefined, false
true: all values other than that belong to false, empty object {}, empty array [], true
Function
9. Anonymous Function: A function defined without a specific name to call it. Such functions usually are not supposed to be called at many places rather, written inside the place where it needs to be executed immediately.
function( ){
console.log( “I love You” )
}
10. Arrow Function: A simple and short way of writing functions.
const sum = ( num1, num2 ) => {
const result = num1 + num2;
return result;
}
To call it sum(2, 3) //result will be 5
Writing the function in a regular way,
function sum ( num1, num2 ) {
const result = num1 + num2;
return result;
}