JavaScript Fundamentals

Strings and Template Literals

 
String is a sequence of characters and is used to store text. But what if our text is dynamic and to print it nicely we require a lot of effort; i.e. to take care of the spaces required before and after one text.
 
For example,
 
 
But what if I say there’s an easy way to do this and that is through Templates Literals? We can write dynamic string in one line like we write a simple English sentence. To use template literal make use of “ ` ”.
 
 
Now there’s one more use of template literal. What if we want to write a sentence in more than one line? Then in case of string we have to use backslash \ n  whereas we do not need anything while using template literals.
 
 

Type Conversion and Type Coercion

 
Type conversion is where we have to manually convert from one data type to another
 
For example,
 
 
But we do not need to perform this manually because JavaScript in real time converts these automatically for us which is known as type coercion.
 
For example,
 
 
Since 23 is a number but JavaScript converted it to a string because of the plus operator between string and number, thus the type coercion took place.
 
Now what if we have minus operator. What will happen?? Ever imagine? Let’s find out.
 
 
Now consider the case of converting the Numbers or Strings to Boolean.
 
Always remember these five values will always give you false value and other than this they will return a true value.
 
null, ‘‘ , undefined, 0, NaN
 
 

Equality Operator (== vs. ===)

 
Triple equals (===) is known as strict equality operator which strictly compares the values i.e. which does not perform type coercion.
 
 
Double equals (==) is known as loose equality operator because it does type coercion before comparing the values.
 
Example,
 
 

Functions

 
Function is a block of code which can be used again and again if needed instead of writing the same lines of code again.
  1. function sum(number1, number2){  
  2.    return number1 + number2 ;  
  3.  }   
Now, function is a keyword used to define a function.
 
sum is the name of the function.
 
number1 and number2 are the parameters of the function named sum in which the value of arguments get passed.
 
This function returns some calculated value got from adding two numbers.
 
Now there are some ways of defining the function.
 
Function declaration
 
 
Function Expression
 
 
Difference between function declaration and expression is “hoisting” which we will discuss in coming articles, as this article is all about the JavaScript Fundamentals.
 
Example
 
 
What happens here is in Function Declaration we can call the function before function declaration but in case of function expression we cannot call the function before. This is because of hoisting which we will discuss later.
 
Exercise
 
There are two teams, Team1 Angular and Team2 React. Each team competes 3 times, and the average of the 3 scores is calculated.
 
A team only wins if it has at least double the average score of the other team. Otherwise, no team wins!
  • a. Create a function ‘calculateAverage’ to calculate the average of 3 scores.
  • b. Create a function ‘getWinner’ that takes the average score of each team as parameter ad then logs the winner to the console, together with the victory points.
Test Data 1. Angular scores 85,54 and 41 whereas React scores 23,34 and27
 
 

Summary

 
This article contains some fundamental terms of JavaScript which one should have the knowledge of. In coming articles we will move from basics to advanced and will be solving some JavaScript coding challenges as well. So stay tuned!!!