Hello folks!
I have planned to complete the JavaScript curriculum from the freeCodeCamp. I have always wanted to complete a course on JavaScript but couldn't do so due to procrastination, laziness and a lot of other reasons.
But this time, I feel that not only learning but keeping a record of each concept and topic, will keep me motivated to continue the course and complete it until it ends. Another reason to choose the freeCodeCamp was to come across this playlist by Florin pop. He has done an amazing job there.
This will be a series of posts, which would include different topics, involved in the freeCodeCamp's JavaScript curriculum. Now, without any more delay, let's start with topics.
Comments
We can comment JavaScript code using //
or /* */
. Although both of them are valid comment specifiers. We use //
for single-line comments and /* */
(start with /*
and end with */
) for multi-line comments. For eg.
// Single Line or inline comment
/*
Multiple
Line of
Comments
*/
Declaring and Initializing Variables
We know, that variables are used to store data. In JavaScript, we mainly have seven kinds of data types which are
undefined
null
boolean
string
symbol
number
object
JavaScript is also a dynamically typed language i.e. a variable in JavaScript can hold any type of value at any point of time within a program. We use the var
keyword to declare variables in JavaScript as
var myName;
Here myName
is a variable, which can store any data type from the above list. Also, don't forget to end a JavaScript statement with a semicolon(;
).
Variables in JavaScript can contain numbers, letters,
$
,_
but can't contain spaces and start with numbers.
We can as well assign a value to the variable using assignment =
operator as
myName = "Prashant";
You can also initialize a variable at the time of its declaration as
var myNum = 7;
var anotherNum = myNum;
Quirks with uninitialized variables
- When you don't initialize a variable, rather just only declare it, they hold the value
undefined
. - Performing any mathematical operation with
undefined
will result inNaN
which means Not a Number. - Performing concatenation with
undefined
by adding a string literal to it will result in a string"undefined"
.
JavaScript is a case-sensitive language. It means variable myvar
and myVar
are different due to case insensitivity of letter v
. In JavaScript, the best practice to define variables is to define them in camelCase.
Mathematical Operations in JavaScript
Number
data type is used to represent numeric data. There are various operators in JavaScript.
- We can add two numbers in JavaScript using
+
operator as
var a = 10 + 10; // assigned 20
- We can subtract two numbers in JavaScript using
-
operator as
var b = 15 - 5; // assigned 10
- We can multiply two numbers using
*
operator as
var c = 5 * 10; // assigned 50
- We can divide two numbers using
/
operator as
var d = 80 / 10; // assigned 8
Until now, we have just used some basic mathematical operation which we've seen already in mathematics. However, there are some other helpful mathematical operators like
- Increment a number by 1 using
++
operator. Yes, I hear, you say this can easily be done as
var = var + 1;
However, JavaScript provides a shorthand to achieve it without the need of an assignment operator. The above statement is equivalent to
var++;
- Decrement a number by 1 using
--
operator. Similarly, as the increment operator, we can decrement a numerical value stored in a variable in a concise way as
var--;
Some other operations are
- You can create Decimal numbers by just assigning the variable to the floating value. For e.g.
var pi = 3.14;
is a valid decimal number. All other arithmetic operations described above can also be performed with decimal numbers. :)
- We have remainder operator i.e.
%
in JavaScript to find the remainder of a division. For e.g.
var rem = 7 % 3; // assigned 1
This operation is widely used for finding out even and odd numbers as a number properly divisible by 2 would be called as even, and odd otherwise.
JavaScript provides some shorthand for compound assignments for various mathematical operations like, +=
, -=
, *=
, /=
etc. You can use them as
var a = 1;
a += 5; // a = a + 5
Other shorthand operators work in the same fashion.
Conclusion
In this post, we've got familiar with concepts like comments, variables and various mathematical operations in JavaScript.
References
Let's meet in the next post, covering some other JavaScript fundamentals. Till then, be curious and keep learning! :)
Top comments (0)