logo

NJP

#2 Statements And Syntax in JavaScript | Learn JavaScript with ServiceNow

Import · Nov 16, 2020 · video

[Music] please subscribe to my channel and click on the bell icon to get the regular updates of my channel and do not forget to like comment and share hello everyone welcome back to sas with servicenow this is part of training for learning javascript with servicenow the next topic is statements and syntax in this section we will learn what are javascript statements components of statement what is javascript syntax starting with what are javascript statements javascript statements are list of instructions which needs to be executed to perform some action these instructions are called as programming statements and we call them as code as well you can write a program which can have list of programming statements let me show you in my personal developer instance of service now i will be using background script for this so let me write a code so i will start with where a equal to 100 where b equal to 400 where c equal to a plus b and then i can do gs dot print here i can print the value for c now all these four lines which we have written all these four lines of code they are written in javascript and these lines of code are called as individual statements so we have one statement two statement three statement and then four statement total we have four statements now all these four statements they make one program that means it's overall one program so one line of code is called statement in javascript now this program will provide some output when you will execute it so this whole program if i will execute it it will return some output so if you will go a little bit bottom you will see here we have this run script button so if i click on this button you will see the output as 500 and this 500 is basically the sum of the values stored in a and the value stored in b so in this code we have used the statements to declare and sum two variables components of javascript statements javascript statements consist of values operators expressions keyword and comments this is one line of code in which 100 is a value equal is an operator where is a keyword and it also has variable x it is not mandatory to have all components in one line of code ending a line of statement in the same code you can also see semicolon which is basically added at the end of the line of this statement semicolon is used to complete one line of statement or code it is also used to separate other lines of code with each other i will write a line of code or statement which will have these components so starting with where which is a keyword to define a variable then i will put x which is a variable then i will put equal to which is an operator and then i will mention hundred which is the value of the variable x now this is the line of code however in order to complete this line of code i will mention semicolon in the end then it will be called as complete statement of a program and it is ready to be executed so if i would like to print this so like i do gs.print and i just print x semicolon so this is the second line of code both are separated and if i come over here if i click on run you will see i'm getting the output 100 that means we had two different statements and we ran the program both the codes got executed and we got the output as well now in javascript spaces are also ignored so if i go back if i come over here let's say if i write where x equal to and i put says now now here i'm not putting any space so i'm doing like where x equal to sas now and if i come over here and i put where x equal to and i give some space let's say and then i write sas now and maybe i will put it as y now both are same so even if i put x over here there's no difference even you have spaces over here so javascript it will always ignore the spaces and it will still execute as it is so system will still think that this x has this value it will ignore all the spaces we have over here and even if here you don't have any space it will automatically treat this as a value string value but at the same time over here as well even if you have multiple spaces system will ignore that and that's what you do in javascript so overall javascript ignore spaces what is syntax in javascript javascript syntax is set of rules to construct the javascript programs you can define how program will be written in javascript let's say you are using fixed value and that value is a number now you can write number value in decimal or non-decimal it is similar for strings when you want to provide fixed value of text then you can write strings with single quotes or in double quotes now these fixed values are called as literals in javascript let's say i need to print a number and text so i will write gs dot print and i will put 100 and then i will put gs.print i will put a string that is sas now and semicolon now if i will run this code this will be printed as is so you can see that we have these fixed values now you can only change them by replacing these values in the script overall you're not using any kind of variable so the only way to change these values you have to change directly from here and that's the reason these fixed values are called as literals in javascript another type of value which can be mentioned in javascript as variable so if i remove it from here and let's say i type where a equal to hundred and i do where b equal to sas now now here a and b are variables they can have different values as well so let's say first we have where a equal to 100 where b equal to sas now so in that case what i will do i will first write maybe gs dot print and i will print a and i will do gs dot print and i will put b now here i will change the value of these variables that's initially if i talk about literals there you had fixed values but in variables you can change the value so if i do let's say a equal to 200 you can do b equal to i can maybe do sas now channel semicolon and i do gs dot print i can print a again and i can print b again now you will see the difference so if i will run this code you will see first it printed 100 that is the value of a then it printed sas now which is the value of b variable then it printed 200 the reason behind it because we have changed the value of the same variable that is a variable and same goes to here we have also changed the value of b variable as well and that's the reason it is printing sas now channel and that's that's the reason this is called as variables overall you can change the values for these variables assignment you can assign values to the variables with equal operator which means variable stores that value mentioned in the statement if i need to assign a value to a variable so let's say if i do where and we have a variable a now if i have to assign a value to this variable a then i just need to use equal operator and i will just put 100 maybe any value even 1000 10 000 whatever value i want to provide i just need to use equal operator and that's how you do assignment in javascript one of the component of javascript statements is expression now expression is created with variables values and operators which also make another value after combining them so let's say i write where x equal to 100 and then i write where y equal to 400 semicolon and i do where z equal to x plus y semicolon now this x plus y is an expression in which two variables are combined with a plus operator what operator this plus operator and which gives a value as an output you can also provide comments in javascript statements comments are line of statement in a javascript program which is not executed comments are used to provide details of any line of code or section of code you can use comments when you do not want to execute a specific line of code which is normally done by developers while debugging of a code of a program there are two types of javascript comments single line comments multi-line comments single line comments starts with double forward slash and then you can write any statement string or code which is not needed to be executed multi-line comments starts with one forward slash with asterisk and then you can provide the statements and strings in multiple lines in order to consider all the lines as comments you need to put asterisk and one forward slash after the completion of all the lines so system will not execute the statements between these two tags if i write where x equal to 10 and then i put semicolon so this is one line of statement which is ending with semicolon now i can write second statement of code in the same line or in the next line it is still the same and treated as second statement so if i write over here where b equal to 100 semicolon then this the second part is basically the second line of code the reason behind it we have already used semicolon to separate these two statements now if i need to add comments for that i can put forward slash and i can basically put any string i want or let's say in order to understand this comments let me put this where b over here i will just put over here after comments basically comments tag which we have mentioned now what i will do i will try to print both so maybe i will do gs dot print and i will do a comma b and then i will put semicolon now in this case if i will print this you will definitely get some error you know here i have to put x not a then i will just run this if i will run it will definitely give you some error it says b is not defined the reason behind it because system is not able to find b you have mentioned b here but it says hey i don't i'm not able to find b where exactly you have defined it because system is not able to read this line because this line is in comments and that's the reason system is not able to read it if i go over here and let's say i i use multiple comments so i put like this and i do asterisk and let's say i do where c equal to 100 uh maybe 500 and here i do [Music] like this and then i put asterisk and now i try to print b i also try to print c this time or let's say we will try to print c now if i run this code you will see i'm still getting the error that hey c is not defined because i'm not able to find it it says unterminated comment okay let's see that ah the reason behind it because we have mentioned star over here so estes should come over here and now i can run it and you will see a different error see it says c is not defined caused by error here that means it's it is trying to run the code which is over here which is also not in comments but it is not able to find the c value it is not even defined over here and that's the reason it is giving you this error overall if you do not want to run any specific line of code then you can just use single line comments and multi-line comments javascript is also case sensitive so if i write where a equal to sas now in capital letters and then i do where b let's use a equal to and i put sas now now here both the a are different so they they have it seems like they have same value but this is in capital letters and this is in lower case so both are different values for these two variables i can also put it as a b but if i maybe check whether a is equal to b or not basically that won't come as true so for example if i put a equal to equal to b is it true then print yes else no so program itself will tell you whether they are equal or not so i can show you so i can put over here gs.print else it will be no and i do semicolon over here semicolon over here and i will run the script so we got no that means a and b are not same and the reason behind it because it is in capital letters and this is in lower case letters so in that case if i change this let's say if i make it sas now what answer we will get will it print yes or no this time it will print yes because they have same values and also both are in upper case so if i run this you will get the yes answer so the overall javascript is case sensitive

View original source

https://www.youtube.com/watch?v=HKm57uCP_oo