Starting with JavaScript

Introduction

  • So this blog does not include a mini project of any sort instead it contains my progress with JavaScript.

  • I've only just started learning JavaScript so i don't know a whole lot.

  • Considering the purpose of this blog is to document my learning process here we go.

What i learnt

Where to put the code (How to practice)

  • Generally to make JavaScript work you would need to link it with the HTML, but if we just need to test out some code you don't have to.

  • If we use the chrome developer tools we can go to sources and then snippets.

  • here we can add a snippet of JavaScript and then just try it out.

  • This is easier than just using the console on the developer tools as its more convenient to make multi-line code snippets.

Random number generator

  • JS has a random number generator that generates a pseudo random number between 0 to 1 not excluding 1.

  • The number generated generally has 16 decimal digits.

  • When we want to generate other numbers we generally multiply it by the number up to which we need and round the number or do something similar to get the range of values required.

var num=Math.random(); range //0 - 1
num*=6; //0 - 6 (does not include 6)
num=Math.floor(num);  //0-6 (only whole numbers and excludes 6)
num=num+1;//1-6 including 6 and 1
  • The above is the code for a simple dice throw.

Strings

  • Strings are basically just a collection of characters.

  • We can do a lot of operations to manipulate strings like :-

    • Concatenate

    • Slice

    • length

    • capitalise

  • We can concatenate two strings by simply using the "+" operator.

  • We can slice a string i.e. Extract a part of a string using the slice() function that take the inclusive from index and exclusive to index of the string to be sliced.

  • The length of a string can be extracted using the .length() function.

  • You can make the string all upper case or all lower case by using the toUpperCase() and toLowerCase() functions respectively.

User Interactions

  • You can interact with the user in 2 main ways and maybe a third way which is not that straight forward.

  • One is using the alert() function that will show a pop up that will have the message with whatever was in the parenthesis.

  • prompt() on the other hand will still give you a pop up but it will ask you to enter a value which can be used later.

  • The third way (kinda) is console.log() this will print out whatever is in the parenthesis on to the console.

  • This is not generally seen by users but its useful for developers.

Conclusion

  • Today i only just started learning javascript which i've heard to be a very important part of cyber security.

  • Any tips and help to learn would be appreciated thank you