10 Things I Learned as a JavaScript Noob.

  1. JavaScript doesn’t have different data types for numbers. By default, all numbers are considered double. There is no integer. For example, suppose you declare a variable two variables,a = 7 and b = 2 they’ll be float by default. In other programming languages like c, c++ would give a/b = 3 but in JavaScript, you’ll get result 3.5. JavaScript by default does floating-point division for all numbers.

2. === vs == operator for comparison. If you’re familiar with other programming languages and new to JavaScript you might frown upon ===. Long story short double equal operator is somewhat forgetting. It tries to coerce variables if the data type is not the same on both sides of ==.

If we have two variable num1 = 5 and num2 = "5" using == equality will give the result true. Though we know that number 5 and string 5 is not equal but JavaScript courses “5” to number 5 for comparison.

On the other hand === will not work if the data type of the variables/values on both sides is not the same.

If we have two variable num1 = 5 and num2 = "5" using === equality will give the result false. We know that number 5 and string 5 is not equal. === checks both values and data types for the condition to be true.

Similar thing applies to != and !== operator.

3. Coma iring strings in JavaScript. Suppose you’ve two string ‘Bangladesh’ and ‘bangladesh’ now you want to compare these two strings. For this comparison you’ve 3 operators >, < and ==. For now, we’ll focus on the == operator.

== will compare both strings in a case-sensitive way. The case-sensitive way means each character at the same place of both strings needs to be the same case for the condition result to be true.

For considering country1 and country 2 variable same you’ve to make both of them to the same case, either lowercase or uppercase. JavaScript has two methods toLowerCase() and toUpperCase() for converting string case. In the following example, we’ve converted country1 variable to lowercase using country1.toLowerCase()

4. Two-way to make an array in JavaScript. Using traditional [] seen in other programming languages and using Array class.

Using [] we declare an array with values separated by ,. When we use Array() we declare an empty array then we assign values of the array using index and value.

5. Using an array as Stack and queue. We can use JavaScript arrays as Stack and Queue. We’ll explore stack now.

The stack is a First in First Out data structure. First, we declare a stack, and then we can use the array push and pop method to inserting and popping stack items.

6. 3 keywords in JavaScript for variable declaration var, let, and coast.

Using const we can declare a variable in JavaScript the value of which won’t change. The usual convention to declare a constant variable is using all caps for naming. Like const PI = 3.14159 .

var is used to declare a variable which we can reinitialize again and has scope current context and closure thereof.

let is used to declare a variable that has scope within a declared code block.

7. forEach() for array iteration.

Suppose we’ve declared an array of fruits. For iterating over array items we type the array name then a dot and forEach this way fruits.forEach() inside forEach we’ve to supply a function. The function takes 3 parameters. For getting items we can use the first parameter.

Here we’ve given it the name fruit. Using this name inside the loop we’ve printed the array items.

For getting an index of items we can use the second parameter. Here we’ve used the index name for the second function parameter in line 13 below and printed the index and item inside the loop.

8. Removing whitespace from strings.

In web development when people enter text in the input we often encounter unnecessary whitespace at the beginning or end of the strings.

For getting rid of these things JavaScript strings have few properties like trim() which removes whitespace from both sides of a string, trimStart() which removes from the beginning andtrimEnd() removes from the end.

9. ternary operator. If you’re familiar with if-else condition in JavaScript you can use the same logic using the ternary operator to write a more readable and concise code.

For this reason, we need to use two things? And:. Before? we write our condition which we usually write inside if(). After? We write the code that we usually write after the if() inside {} or without {}. At last, after we write the code which we usually write inside the else {} or after else. You can see below the example of converting ifelse to ternary operator.

10. Math function for mathematical function and constants.

In programming, we often come across when we’ve to use mathematical functions and constants. JavaScript has a handy class math which you can use for becoming a maths wizard.

Math.PI will give you constant value pie, Math.LN2 will give you the natural log of 2.

For calculating sqrt you can use Math.sqrt(value), for calculating the power of a value we can use Math.pow(value, exponent) for calculating absolute value we can use Math.abs() , for the ceiling of a value Math.ceil(value) and finally for calculating floor Math.floor(value)

Your valuable feedback and suggestions are welcomed.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store