Javascript Concepts …

shadreza
5 min readMay 5, 2021

Before going into details let’s know what is Javascript?
Some also call it JS. And many may find it as an extension of Java.
Well that not true. Javascript is at first a programming language.

This is such a language that is profoundly used in web applications and makes them interactive. Well in addition to that we can say when a plant doesn’t get water for many days it becomes very dry but if it is given water then it really gets back its life. Similarly, we can say the HTML and CSS being the dry plant that needs Javascript for its flourishment and being green again in other words to make a website interactive then Javascript is a must.

And another misconception is that it is not an extension of java rather the thing is that it is a scripting language when it came out java was very popular so the name came as javascript. And if we say for similarities the implementation of OOP [Object Oriented Programming] is the biggest match of the bunch.

As a programming language, JS is very popular as the usage of telecommunication and the internet has been a burst and everything must have a web version of it. So Javascript must have the tools that can fix our problems, share our thoughts, and do as we like…

Well, Javascript works with a lot of data types like string, number, boolean, array, and so on. Being a basic conceptual blog I won’t talk about the hard parts rather quite simple ones…

Starting with strings.

Strings are nothing but we can easily explain as words. It may be a letter, maybe a single word[may or may not be meaningful], maybe many words, or even a sentence. In a simple sense when we are working with words we mean to work with strings. Mainly we cover a string by quotations [ ‘ ’ or “ ” ].

let message1 = "this is a string that can be changed because of let";
console.log(message1);
const message2 = "this is a string that can not be changed because of const";
console.log(message2);

  1. Making a string all uppercase
    There may come times when we have to make all the letters of the string uppercase. Then we will use a method called toUpperCase() that will uppercase the total string. A question may arise if there is a number in between then what will be the case. Well just do it in the console. It will uppercase only the letters, not the numbers or other symbols.
    let message1 = "this is a string that can be changed because of let";
    console.log(message1.toUpperCase());
  2. Making a string to lowercase
    Similarly like uppercase, there is another method for lowercasing the string. This will lowercase the uppercased letters of the string. Can you guess the method???
    Well, it's toLowerCase() method that does the trick.
    let message1 = "THIS is a StrinG that can be MODIFIED to 123";
    console.log(message1.toLowerCase());
  3. Adding two strings together
    This is called string concatenation. What this does is that it takes two operands or strings and joins them into a string that has both parts in the joining order.
    let string1 = "Welcome";
    let string2 = "To A New World";
    let concatenatedString = string1+string2;
    console.log(concatenatedString);

    Well, here the (+) operator does the work. But in the output, we see that Welcome and To are so closely joined to remove that just give space at the front of T [ “ To” ] or after e [“Welcome ”].
  4. Trimming a string
    Well if we are given a string like the following
    let message = " Spaces before and after ";
    Then if we want to vanish the spaces at the front of the end then we will have to use trimStart() & trimEnd() methods respectively.
    like if we want to vanish both the spaces then we may do like this
    message = message.trimStart();
    message = message.trimEnd();
    console.log(message);

    You may reverse then at first the ending space will vanish then the starting one. So this is how you can remove starting and ending spaces from a string.

Much about string now let's talk something on numbers, shall we?

  1. Generating number from string
    Suppose we have a string that contains a number but because of the “ ” quotations we cannot perform tasks like we can on a number so is there any way to convert that string to a number? Yes, the parseInt() or parseFloat() method does the trick.
    let a = '123.23';
    console.log(parseInt(a));
    console.log(parseFloat(a));

    I hope you got the point when to use parseFloat and when parseInt. Well to just add when you don’t want the decimal points then go for parseInt() and when the fraction points are needed then use parseFloat()
  2. Knowing the remainder
    Well, when it comes to numbers if someone asks you if a number even or odd then what do you do?
    You divide the number by 2 and see if the remainder is 0 or 1 ending up to even or odd eventually. So how to calculate this remainder?
    Just simply use % [modulus operator].
    let a = 123;
    console.log(a % 2);
  3. Want to show a number in scientific or exponential format
    If you are working with numbers and want to turn them into a scientific number that is expressed by exponential then simply use toExponential() method.
    let a = 123.456;
    console.log(a.toExponential());
  4. Whether a number is an integer or not?
    If it comes to our mind if the given number is an integer then what can we do to code it? Cause we won’t be there looking at the number always and telling the user that please the phone number must be an integer or the order iid has to be an integer. What we can do is that simply use isInteger() method that comes from the Number library and answers us in true or false. If true then it is an integer else, not an integer. Just what we have to do is that
    let a = 1;
    console.log(Number.isInteger(a));
    let b= 1.2;
    console.log(Number.isInteger(b));

    As isInteger() method is coming from Numbers library so Numbers.isInteger() is the main thing here.
  5. If we have to fix a floating number within a position
    When it comes to floating numbers the rational and irrational numbers may be very long after the dot(.) sign meaning it may have many decimal points in the floating part. In order to fix that we use toFixed() method.
    let a = 123.4567;
    console.log(a.toFixed(2));

    Here we have to tell the method how many digits do we want to fix. Like in my example I fixed it up to 2 digits. You can do any as you like but the point to be noted is that it will round u the result so that's one thing to take into consideration when using this method.
  6. Converting Number To String
    In the beginning, we had seen how to convert a string to a number and now we will see the reverse one. This will be done by the toString() method.
    let a = 123.4567;
    let b = a.toString();
    console.log(b);

    Here the variable b is a string that has the number a within but will act as a string.

Well, I think these are quite basic stuff that we learned today. And I hope we could understand the use cases.

Hope to meet you lovely people soon.

Till then its goodbye from your friend,

shadreza

--

--

shadreza

Hello beautiful people of the internet. Hope to share nice stuff with you...