top
upGrad KnowledgeHut SkillFest Sale!

Search

JavaScript Tutorial

JavaScript syntax is basically a set of rules, which governs how programs are constructed.Variables in JavaScriptJust like any other programming language, variables are used to store values.In traditional programming languages like C, C++ and Java we declare variables by their type i.e. int, double, string, boolean.But in JavaScript, we use the keyword var to declare all variables. Later when we assign values to it, the compiler decides it type.Consider the below example. Here we are first declaring a variable num, then assigning a value 10 to it. The compiler will now mark it as Number datatype. Similarly, we are declaring variable str and assigning a string value to it in the next line. Notice the bool variable. Here we are both declaring and assigning a value at the same time.var num; num = 10; var str; str = “I am a string”; var bool = true;Operators in JavaScriptJavaScript uses arithmetic operators like + - * / to compute values. It also have other operators to compute like increment(++) and decrement(--) operator. It also have two comparison operators(== and ===).The assignment(=) operator is used to assigning the right-hand side expression to the left variable.We will learn in detail about the operator in the chapter on Operators. Let’s consider the below example now to watch the above-mentioned operators in action. You can run the below code in any browser’s console.var a = 10; if( a === 10) {  var res = ++a + (10 * 6);  console.log(a); //11  console.log(res); //71 }IdentifiersJavaScript identifiers are used to name variable. Like in most programming languages, we have rules to name variable.Keywords are not allowed as identifiers. The first character must be a letter, or an underscore (_), or a dollar sign ($).Subsequent characters may be letters, digits, underscores, or dollar signs.Variable names are case-sensitive. So, firstName and firstname are different.So, by following the above rules we can create variable names. But if there are multiple words, which needed to be joined then JS developers and the community follows the lower Camel case notation. Example is –firstName, lastName, fullNameCommentsLike all other programming languages, we all have comments in JavaScript. Comments are skipped by the JS compiler and not executed. Developers generally use comments to make their code more readable and understandable by other developers.There are two types of comments in JS- Single line comment and Multi-line comments.Single line comment starts with //. Any text between // and end of the line will be ignored by JS compiler and will not be executed.Multi-line comments start with /* and end with */. Any text between them will be ignored by JS compiler and will not be executed.Below is an example using both Single line comment and Multi-line comments./*Below code checks whether the age passed is above  then 18 or not */ if(age > 18) { return “Adult”; //Will return the string “Adult” } else { Return “Not adult”; //Will return the string “Not Adult” }
logo

JavaScript Tutorial

Syntax

JavaScript syntax is basically a set of rules, which governs how programs are constructed.

Variables in JavaScript

Just like any other programming language, variables are used to store values.
In traditional programming languages like C, C++ and Java we declare variables by their type i.e. int, double, string, boolean.

But in JavaScript, we use the keyword var to declare all variables. Later when we assign values to it, the compiler decides it type.

Consider the below example. Here we are first declaring a variable num, then assigning a value 10 to it. The compiler will now mark it as Number datatype. Similarly, we are declaring variable str and assigning a string value to it in the next line. Notice the bool variable. Here we are both declaring and assigning a value at the same time.

var num;
num = 10;
var str;
str = “I am a string”;
var bool = true;

Operators in JavaScript

JavaScript uses arithmetic operators like + - * / to compute values. It also have other operators to compute like increment(++) and decrement(--) operator. It also have two comparison operators(== and ===).

The assignment(=) operator is used to assigning the right-hand side expression to the left variable.

We will learn in detail about the operator in the chapter on Operators. Let’s consider the below example now to watch the above-mentioned operators in action. You can run the below code in any browser’s console.

var a = 10;
if( a === 10) {
 var res = ++a + (10 * 6);
 console.log(a); //11
 console.log(res); //71
}

Identifiers

JavaScript identifiers are used to name variable. Like in most programming languages, we have rules to name variable.

Keywords are not allowed as identifiers. The first character must be a letter, or an underscore (_), or a dollar sign ($).

Subsequent characters may be letters, digits, underscores, or dollar signs.

Variable names are case-sensitive. So, firstName and firstname are different.

So, by following the above rules we can create variable names. But if there are multiple words, which needed to be joined then JS developers and the community follows the lower Camel case notation. Example is –

firstName, lastName, fullName

Comments

Like all other programming languages, we all have comments in JavaScript. Comments are skipped by the JS compiler and not executed. Developers generally use comments to make their code more readable and understandable by other developers.

There are two types of comments in JS- Single line comment and Multi-line comments.

Single line comment starts with //. Any text between // and end of the line will be ignored by JS compiler and will not be executed.

Multi-line comments start with /* and end with */. Any text between them will be ignored by JS compiler and will not be executed.

Below is an example using both Single line comment and Multi-line comments.

/*Below code checks whether
the age passed is above 
then 18 or not */
if(age > 18) {
return “Adult”; //Will return the string “Adult”
} else {
Return “Not adult”; //Will return the string “Not Adult”
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Janani

I have learned many things from this article. It is beneficial for me. Thank you!

Nilesh Chakrabarty

Nice example for beginners.. I m a beginner so this is very helpful for me ... so plz give this type of beginners example..

michael

This is a great introduction to variables in JavaScript! As a beginner to JavaScript, I found this guide very helpful in understanding the basics of variables and how they are used in JavaScript.

qtsinfo it

Thanks for sharing the information, it is very helpful, I hope to get more such beautiful blogs from you.

knowledge-wisdom

You have shared great information with me i appreciate your work!

Suggested Tutorials

Node JS Tutorial

Build various types of web applications,command-line application,etc.
Node JS Tutorial

Build various types of web applications,command-line application,etc....

Read More

Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introduced by Google corporation in 2012 and was considered to be one of the most promising among JavaScript frameworks. It was written completely in JavaScript to separate an application’s logic from DOM manipulation, aiming at dynamic page updates. Angular introduced many powerful features enabling the developer to effortlessly create rich and single-page applications.Topics CoveredThis Angular tutorial will span over eight modules, each module covering numerous individual aspects that you need to gain complete information about Angular. This set of modules serves as an Angular tutorial for beginners along with experienced IT professionals.Here are the topics that will be covered in the Angular tutorial:Get started with Angular.Learn the basics of Angular.Know what Angular Directives.Get an idea of Component Inputs and Outputs of Angular.Know about Forms in Angular.About Services in Angular.Pipes in Angular.HTTP, Routing and Building in Angular.Who can benefit from this tutorial?This Angular tutorial will be helpful to IT professionals such as:Software Developers, Web Application Programmers and IT Professionals Software Architects and Testing Professionals Career aspirants in web development
Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introdu...

Read More