Skip to main content

Write Your First JavaScript Program

Hey there! in this article we are gonna write a simple basic program of javascript. Don't worry it uses only two fine methods which are quite essential for everything and will be explained in the easier way possible.

Today we are gonna do Multiple Of Any Number From 1 to 10 that's it!

    Code

    for(let k=0 ; k<=10 ; k++)
    {
        if(k%2 == 0)
        {
            console.log(k)
        }
    }

    Output:

    0
    2
    4
    6
    8
    10

    So to do this we are gonna need to know about two simple concepts and will try to make it as simple as possible too.

    Code Analysis:

    • So basically in a for loop in the place of condition, we'll initialize the condition, for example, let k=0 this means to make my multiple of any number beginning with zero.
    • Seconds, k<10 why 10? we are gonna do multiples up to 10 right? so k less or equal to 10 takes effect.
    • Third, k++ means to increment or increase the initialized k=0 as it progresses through the for loop body every time.
    • We are taking a for loop and having a condition that in simple English "let k start from zero and also let k less or equal to 10 because we need number up to 10 or it's your choice and also let k increment after each entry into its body".
    • So if statement is an easy concept to put it simply.
    • It is basically saying if this is present go inside my body or you are not allowed.
    • Now each time it enters into the body there is an if statement which says to explain in simple English again "Oh you came into the body so you are less than 10 ok, so to get multiple of any number we know from basic math that by division the remainder has to be zero so anything that comes inside has to deal with division and get remainder(%) zero, So when zero if it is equal(==) to zero you are allowed to enter inside my if statement body".
    • Inside the if statement body we are using the same old console.log to print the value of k during the whole iteration.

    Note: If you want multiple of any other number other than 2 you can change it in the if statement condition place. Check the code out and play with it in your own creative ways and don't worry if you are still not able to get it, we will cover each concept in more detail later.

    Tags
    Submitted by arilio666 on September 9, 2021

    Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

    About

    At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.