0% found this document useful (0 votes)
2 views13 pages

Javascript Notes5

The document provides an overview of JavaScript syntax, covering its rules and conventions for structuring code, including variables, operators, functions, and data types. It explains the differences between fixed values and variable values, as well as the use of comments and identifiers. Additionally, it highlights the ease of learning JavaScript syntax, especially for those familiar with C-like languages.

Uploaded by

Bharath
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
2 views13 pages

Javascript Notes5

The document provides an overview of JavaScript syntax, covering its rules and conventions for structuring code, including variables, operators, functions, and data types. It explains the differences between fixed values and variable values, as well as the use of comments and identifiers. Additionally, it highlights the ease of learning JavaScript syntax, especially for those familiar with C-like languages.

Uploaded by

Bharath
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 13

Skip to content

geeksforgeeks
Courses
Tutorials
HTML/CSS
JavaScript

Sign In

DSA with JS - Self Paced


JS Tutorial
JS Exercise
JS Interview Questions
JS Array
JS String
JS Course
JS Object
JS Operator
JS Date
JS Error
JS Projects
JS Set
JS Map
JS RegExp
JS Math
JS Number
JS Boolean
JS Examples
JS Free JS Course
JS A to Z Guide
JS Formatter

GfG 160
Share Your Experiences
JavaScript Tutorial
JavaScript Basics
Introduction to JavaScript
JavaScript Versions
How to Add JavaScript in HTML Document?
JavaScript Syntax
JavaScript Output
JavaScript Comments
JS Variables & Datatypes
JS Operators
JS Statements
JS Loops
JS Perfomance & Debugging
JS Object
JS Function
JS Array
JS String
JS Numbers
JS Math
JS Map
JS Set
JS Objects
JS Advance
JavaScript Exercises
Full Stack DevelopmentCourse
JavaScript Syntax
Last Updated : 12 Aug, 2024
JavaScript syntax refers to the rules and conventions dictating how code is
structured and arranged within the JavaScript programming language. This includes
statements, expressions, variables, functions, operators, and control flow
constructs.

Syntax

console.log("Basic Print method in JavaScript");


JavaScript syntax refers to the set of rules that determines how JavaScript
programs are constructed:

// Variable declaration
let c, d, e;

// Assign value to the variable


c = 5;

// Computer value of variables


d = c;
e = c / d;
JavaScript Values
There are two types of values defined in JavaScript Syntax:

Fixed Values: These are known as the literals.


Variable values: These are called variables
These are the features of JavaScript which have some predefined syntax:

Table of Content

JavaScript Literals
JavaScript Variables
JavaScript Operators
JavaScript Expressions
JavaScript Keywords
JavaScript Comments
JavaScript Data Types
JavaScript Functions
JavaScript Identifiers
JavaScript Literals
Syntax Rules for the JavaScript fixed values are:

JavaScript Numbers can be written with or without decimals.


Javascript Strings are text that can be written in single or double quotes.

let num1 = 50
let num2 = 50.05

let str1 = "Geek"


let str2 = 'Geeks'

console.log(num1)
console.log(num2)
console.log(str1)
console.log(str2)
Output
50
50.05
Geek
Geeks
JavaScript Variables
A JavaScript variable is the simple name of the storage location where data is
stored. There are two types of variables in JavaScript which are listed below:

Local variables: Declare a variable inside of a block or function.


Global variables: Declare a variable outside function or with a window object.
Example: This example shows the use of JavaScript variables.

// Declare a variable and initialize it


// Global variable declaration
let Name = "Apple";

// Function definition
function MyFunction() {

// Local variable declaration


let num = 45;

// Display the value of Global variable


console.log(Name);

// Display the value of local variable


console.log(num);
}

// Function call
MyFunction();
Output:

Apple
45
JavaScript Operators
JavaScript operators are symbols that are used to compute the value or in other
words, we can perform operations on operands. Arithmetic operators ( +, -, *, / )
are used to compute the value, and Assignment operators ( =, +=, %= ) are used to
assign the values to variables.

Example: This example shows the use of javascript operators.

// Variable Declarations
let x, y, sum;

// Assign value to the variables


x = 3;
y = 23;

// Use arithmetic operator to


// add two numbers
sum = x + y;
console.log(sum);

Output
26
JavaScript Expressions
Javascript Expression is the combination of values, operators, and variables. It is
used to compute the values.

Example: This example shows a JavaScript expression.

// Variable Declarations
let x, num, sum;

// Assign value to the variables


x = 20;
y = 30

// Expression to divide a number


num = x / 2;

// Expression to add two numbers


sum = x + y;

console.log(num + "\n" + sum);

Output
10
50
JavaScript Keywords
The keywords are the reserved words that have special meanings in JavaScript.

// let is the keyword used to


// define the variable
let a, b;

// function is the keyword which tells


// the browser to create a function
function GFG(){};
JavaScript Comments
The comments are ignored by the JavaScript compiler. It increases the readability
of code. It adds suggestions, Information, and warning of code. Anything written
after double slashes // (single-line comment) or between /* and */ (multi-line
comment) is treated as a comment and ignored by the JavaScript compiler.

Example: This example shows the use of javascript comments.

// Variable Declarations
let x, num, sum;

// Assign value to the variables


x = 20;
y = 30

/* Expression to add two numbers */


sum = x + y;
console.log(sum);

Output
50
JavaScript Data Types
JavaScript provides different datatypes to hold different values on variables.
JavaScript is a dynamic programming language, which means do not need to specify
the type of variable. There are two types of data types in JavaScript.

Primitive data type


Non-primitive (reference) data type
// It store string data type
let txt = "GeeksforGeeks";

// It store integer data type


let a = 5;
let b = 5;

// It store Boolean data type


(a == b )

// To check Strictly (i.e. Whether the datatypes


// of both variables are same) === is used
(a === b)---> returns true to the console

// It store array data type


let places= ["GFG", "Computer", "Hello"];

// It store object data (objects are


// represented in the below way mainly)
let Student = {
firstName: "Johnny",
lastName: "Diaz",
age: 35,
mark: "blueEYE"
}
JavaScript Functions
JavaScript functions are the blocks of code used to perform some particular
operations. JavaScript function is executed when something calls it. It calls many
times so the function is reusable.

Syntax:

function functionName( par1, par2, ....., parn ) {


// Function code
}
The JavaScript function can contain zero or more arguments.

Example: This example shows the use of Javascript functions.

// Function definition
function func() {

// Declare a variable
let num = 45;
// Display the result
console.log(num);
}

// Function call
func();

Output
45
JavaScript Identifiers
JavaScript Identifiers are names used to name variables and keywords and functions.

A identifier must begin with:

A letter(A-Z or a-z)
A dollar sign($)
A underscore(_)
Note: Numbers are not allowed as a first character in JavaScript Identifiers.

JavaScript Case Sensitive


JavaScript Identifiers are case-sensitive.

Example: Both the variables firstName and firstname are different from each other.

let firstName = "Geek";


let firstname = 100;

console.log(firstName);
console.log(firstname);

Output
Geek
100
JavaScript Camel Case
In JavaScript Camel case is preferred to name a identifier.

Example:

let firstName
let lastName
JavaScript Character Set
A unicode character set is used in JavaScript. A unicode covers the characters,
punctuations and symbols.

We have a complete article on character sets. Click here to read Charsets article.

JavaScript Syntax – FAQs


What is the basic syntax of JavaScript?
The basic syntax of JavaScript includes statements, expressions, variables,
functions, operators, and control flow constructs. A typical JavaScript statement
ends with a semicolon and can include variable declarations, function calls, loops,
and conditionals.

What is the syntax for defining a JavaScript function?


The syntax for defining a function in JavaScript is:
function functionName(parameter1, parameter2) {

// Code to be executed

This function can then be called using functionName(argument1, argument2);.

Is JavaScript syntax easy to learn?


Yes, JavaScript syntax is considered easy to learn, especially for beginners. It is
intuitive and has a C-like structure, which is familiar to those who have
experience with languages like C, C++, or Java.

What is the JavaScript syntax for embedding code in HTML?


JavaScript code is embedded in HTML using the <script> tag. The script can be
placed within the <head> or <body> sections of the HTML document, or it can be
included as an external file:

<script>

// JavaScript code here

</script>

What does “syntax” mean in coding?


In coding, “syntax” refers to the set of rules that defines the structure and
format of the code in a programming language. It dictates how code should be
written so that it can be correctly interpreted and executed by the compiler or
interpreter.

Master DSA with JavaScript in just 90 days. Explore core DSA concepts, refine your
coding skills, and tackle real-world challenges. Take on the Three 90 Challenge—
complete 90% of the course in 90 days and earn a 90% refund as a reward for your
commitment!

Comment

More info

Placement Training Program


Next Article
JavaScript Output
Similar Reads
Explain the benefits of spread syntax & how it is different from rest syntax in ES6
?
Spread Operator: Spread operator or Spread Syntax allow us to expand the arrays and
objects into elements in the case of an array and key-value pairs in the case of an
object. The spread syntax is represented by three dots (...) in JavaScript. Syntax:
var my_var = [...array]; Benefits of using Spread syntax: 1. It allows us to
include all elements
4 min read
JavaScript Spread Syntax (...)
The spread syntax is used for expanding an iterable in places where many arguments
or elements are expected. It also allows us the privilege to obtain a list of
parameters from an array. The spread syntax was introduced in ES6 JavaScript. The
spread syntax lists the properties of an object in an object literal and adds the
key-value pairs to the ne
4 min read
What is the Syntax for Declaring Functions in JavaScript ?
Generally, in JavaScript, the function keyword is used to declare a variable. But,
there are some more ways available in JavaScript that can be used to declare
functions as explained below: Using function keyword: This is the most common way
to declare functions in JavaScript by using the function keyword before the name of
the function.Declaring a
1 min read
What is the syntax for leading bang! in JavaScript function ?
Before we get to know the syntax for the leading bang! in a JavaScript function,
let's see what functions in JavaScript are actually are. JavaScript functions are a
set of statements(procedures) that perform some tasks or calculate some value. A
function may take some input and return the result to the user. The main idea to
use functions is to avo
2 min read
How to Create and Use a Syntax Highlighter using JavaScript?
A syntax highlighter is a tool that colorizes the source code of programming
languages, making it easier to read by highlighting keywords, operators, comments,
and other syntax elements in different colors and fonts. In JavaScript, you can
create a syntax highlighter by either manually writing your own code or by using
existing libraries.These are
3 min read
jQuery Syntax
The jQuery syntax is essential for leveraging its full potential in your web
projects. It is used to select elements in HTML and perform actions on those
elements.jQuery Syntax$(selector).action()Where - $ - It the the shorthand for
jQuery function.(selector) - It defines the HTML element that you want to
selectaction() - It is the jQuery method us
2 min read
XML | Syntax
Prerequisite: XML | Basics In this article, we are going to discuss XML syntax rule
which is used while writing an XML document or an XML application. It is a very
simple and straight forward to learn and code. Below is a complete XML document to
discuss each component in detail. XML <?xml version="1.0" encoding="UTF-8"?>
3 min read
Explain the arrow function syntax in TypeScript
Arrow functions in TypeScript are implemented similarly to JavaScript (ES6). The
main addition in TypeScript is the inclusion of data types or return types in the
function syntax, along with the types for the arguments passed into the
function.What is arrow function syntax in TypeScript?Arrow functions in TypeScript
offer a concise syntax for defin
3 min read
Provide the syntax for optional parameters in TypeScript
In TypeScript, optional parameters allow you to specify that a function parameter
may be omitted when calling the function. You denote optional parameters by adding
a question mark (?) after the parameter name in the function declaration.
Syntax:function functionName(param1: type, param2?: type, param3?: type) { //
Function body } Parameters:param1
2 min read
Less.js Extend Syntax & Inside Ruleset
LESS.js is one of the most popular CSS preprocessor languages because of its many
features like mixins, imports, variables, and, so on, which help to reduce the
complexity of CSS code. One such important and useful feature of LESS is the
@extend directive. In this article, we will see the basic usage of the extend
feature in LESS.js, along with kn
2 min read
course-img
86k+ interested Geeks
MERN Full Stack Web Development
Explore
course-img
27k+ interested Geeks
Complete Backend Development Program- Mastering OOPS, Spring Boot, and
Microservices
Explore
course-img
72k+ interested Geeks
JavaScript Full Course Online | Learn JavaScript with Certification
Explore
course-img
52k+ interested Geeks
Data Structures & Algorithms in JavaScript - Self Paced Course
Explore
course-img
154 interested Geeks
Front-End Interview Preparation Course
Explore
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh
(201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar,
Uttar Pradesh, 201305
GFG App on Play Store
GFG App on App Store
Advertise with us
Company
About Us
Legal
Privacy Policy
Careers
In Media
Contact Us
GFG Corporate Solution
Placement Training Program
Explore
Job-A-Thon Hiring Challenge
Hack-A-Thon
GfG Weekly Contest
Offline Classes (Delhi/NCR)
DSA in JAVA/C++
Master System Design
Master CP
GeeksforGeeks Videos
Geeks Community
Languages
Python
Java
C++
PHP
GoLang
SQL
R Language
Android Tutorial
DSA
Data Structures
Algorithms
DSA for Beginners
Basic DSA Problems
DSA Roadmap
DSA Interview Questions
Competitive Programming
Data Science & ML
Data Science With Python
Data Science For Beginner
Machine Learning
ML Maths
Data Visualisation
Pandas
NumPy
NLP
Deep Learning
Web Technologies
HTML
CSS
JavaScript
TypeScript
ReactJS
NextJS
NodeJs
Bootstrap
Tailwind CSS
Python Tutorial
Python Programming Examples
Django Tutorial
Python Projects
Python Tkinter
Web Scraping
OpenCV Tutorial
Python Interview Question
Computer Science
GATE CS Notes
Operating Systems
Computer Network
Database Management System
Software Engineering
Digital Logic Design
Engineering Maths
DevOps
Git
AWS
Docker
Kubernetes
Azure
GCP
DevOps Roadmap
System Design
High Level Design
Low Level Design
UML Diagrams
Interview Guide
Design Patterns
OOAD
System Design Bootcamp
Interview Questions
School Subjects
Mathematics
Physics
Chemistry
Biology
Social Science
English Grammar
Commerce
Accountancy
Business Studies
Economics
Management
HR Management
Finance
Income Tax
Databases
SQL
MYSQL
PostgreSQL
PL/SQL
MongoDB
Preparation Corner
Company-Wise Recruitment Process
Resume Templates
Aptitude Preparation
Puzzles
Company-Wise Preparation
Companies
Colleges
Competitive Exams
JEE Advanced
UGC NET
UPSC
SSC CGL
SBI PO
SBI Clerk
IBPS PO
IBPS Clerk
More Tutorials
Software Development
Software Testing
Product Management
Project Management
Linux
Excel
All Cheat Sheets
Recent Articles
Free Online Tools
Typing Test
Image Editor
Code Formatters
Code Converters
Currency Converter
Random Number Generator
Random Password Generator
Write & Earn
Write an Article
Improve an Article
Pick Topics to Write
Share your Experiences
Internships
DSA/Placements
DSA - Self Paced Course
DSA in JavaScript - Self Paced Course
DSA in Python - Self Paced
C Programming Course Online - Learn C with Data Structures
Complete Interview Preparation
Master Competitive Programming
Core CS Subject for Interview Preparation
Mastering System Design: LLD to HLD
Tech Interview 101 - From DSA to System Design [LIVE]
DSA to Development [HYBRID]
Placement Preparation Crash Course [LIVE]
Development/Testing
JavaScript Full Course
React JS Course
React Native Course
Django Web Development Course
Complete Bootstrap Course
Full Stack Development - [LIVE]
JAVA Backend Development - [LIVE]
Complete Software Testing Course [LIVE]
Android Mastery with Kotlin [LIVE]
Machine Learning/Data Science
Complete Machine Learning & Data Science Program - [LIVE]
Data Analytics Training using Excel, SQL, Python & PowerBI - [LIVE]
Data Science Training Program - [LIVE]
Mastering Generative AI and ChatGPT
Data Science Course with IBM Certification
Programming Languages
C Programming with Data Structures
C++ Programming Course
Java Programming Course
Python Full Course
Clouds/Devops
DevOps Engineering
AWS Solutions Architect Certification
Salesforce Certified Administrator Course
GATE
GATE CS & IT Test Series - 2025
GATE DA Test Series 2025
GATE CS & IT Course - 2025
GATE DA Course 2025
GATE Rank Predictor
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By
using our site, you acknowledge that you have read and understood our Cookie Policy
& Privacy Policy
Got It !
Lightbox

You might also like