Tuesday, July 7, 2020

JAVASCRIPT FOR BEGINNERS


Javascript (JS) Coffee Mug by codegame | Society6

ALERT: JAVASCRIPT BASICS



In order to learn any language, we first hit hard to their root syntaxes. For Javascript, the rule is also the same. Javascript is a scripting language. Download a text editor like Brackets, Notepad++, Atom, Sublime Text or visual studio to name a few. 


RECOMMENDED: I WILL RECOMMEND BRACKETS, AND IT IS MY PERSONAL EXPERIENCE




Hope you are on the editor and ready for hands-on experience. Create a new file on the editor and save it by any name but with an extension of .js  On that note, I will save my javascript file as Vikas.js

Print "Hello world" 
document.write("Hello world");



Syntax for one line comment
// one line js comment



Multiline Comment
/*
multiline js comment
*/



The syntax for show a popup window message
alert("you got a popup");


Variables and data types in Javascript- Strings
First, we will create a variable with keyword var. Here we created var phrase. Variables are like containers in which we store any sort of data. Strings are one of the data types. We denote them with single or double quotes ""
var phrase= "strings are cool";  



Get the length of the string phrase
document.write(phrase.length);  



Convert the string into Upper case/ Capital Letters 
document.write(phrase.toUpperCase);



Find the character by using its index, For those who are new to programming, I want to add that in coding Index is like the address of a character and it starts with 0 not from 1
document.write(phrase.charAt(0));



Find the address/ index of any specific letter
document.write(phrase.indexOf("s"));



Print substring of the string from index 0 to 4 . We can take the desired index. This is called slicing
document.write(phrase.substing(0, 4));



Check whether a particular string is present in the given string
document.write(phrase.includes(cool));



Add two string. This is known as the Concatenation
document.write(phrase + text);  


Numbers in Javascript
Numbers as you know are integers.
number= 5

Print maximum among the 2 numbers
document.write(Math.max(number, 10));


Find minimum among 2 numbers
document.write(Math.min(number, 10));



This syntax will print number raise to the power of any other no. Here 5 raise to the power will be printed.
document.write(Math.pow(number, 10));


Use math module to find the square root of  any number
document.write(Math.sqrt(36));


Generate a random number
document.write(Math.random()); 


Getting user input-this syntax will open a pop window that will ask your name.

var name= window.prompt("what is your name")    

Print the input with desired message.
document.write("hey "+ name + "how are you doing");

Small addition calculator. First, we took 2 inputs and then parsed/ converted them into integers. because the inputs we are getting are strings. so we need to parse them into integers.Finally, add these integer numbers
var num1= window.prompt("enter 1st number");
var num2= window.prompt("enter 2nd number");
num1=parseInt(num1);   

num2=parseInt(num2);
document.write(num1 + num2);

Arrays
var fruits = new Array("cat","dog","cow")
document.write(fruits[]);


Find elements from their indexes. As I told you earlier that index is like the address of a data type. It starts from 0. 
ALERT: TEXT IN GREY ARE JAVASCRIPT COMMENTS NOT THE ACTUAL CODE

document.write(fruits[0]);  //it will return cat


Print the length of the array
document.write(fruits.length);



Add new elements in the array
fruits[0]= "red"
document.write(fruits[0]);  //now it will return red
















Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home