Javascript Programming Cheat Sheet for Beginners - ByteScout
  • Home
  • /
  • Blog
  • /
  • Javascript Programming Cheat Sheet for Beginners

Javascript Programming Cheat Sheet for Beginners

Javascript, or popularly JS, is a text-based programming language. It is popularly used both for front-end and backend programming for client-side and server-side programming. The language is common;y used to impart an interactive element that allows the users to engage with the web pages. In addition, the language is used for updating content, controlling multimedia, and also for animating images.

Javascript allows for creating dynamic and interactive pages by web developers. It is an easy-to-learn language even if you don’t have much coding or programming knowledge and is quite beginner-friendly. Here is a Javascript programming cheat sheet for beginners to help you learn more about the language.

JavaScript CheatSheet for Beginners

JS Data Types

The main codes and data types you should know about JavaScript are:

Console.log() method

You use console.log() to print messages on the console or log messages. It is also used to print objects.

Example:

console.log('Hello World!');
This code snippet prints the message: Hello World!

Data type: Strings

Strings are the most common data type used in JavaScript and can be understood as a string of characters, letters, spaces, numbers, or symbols. A string is always enclosed with single ( ‘ ‘ ) or double ( ” ” ) quotation marks.

Example:

let single = 'This is an example!';
let double = "Here is an example!";

Numbers

Numbers in JavaScript can contain both integers and decimals in floating-point numbers. Hence, you can write whole numbers or fractions.

Example:

let amount = 10;
let price = 9.99;

Booleans

Boolean data types come in only two values. Either variable name can be ‘true’ or ‘false. They are one of the most commonly used data types in JavaScript.’

Example:

let anExample= true;

Null

Null can be described as a data type where a variable is intentionally made to be absent of any value. Thus, the used data type is ‘null’.

Example:

let x = null;

You can assign these data type values by using ‘=.’

Carrying Arithmetic operations

The common arithmetic operations carried by Javascript are:

  • ‘+’ for addition, Example: var a = 1 + 2 + 3
  • ‘-‘ for subtraction, Example: var b = 67 – 8 – 7
  • ‘*’ for multiplication, Example: var c = 10 * 2
  • ‘/’ for division, Example: var d = 12 / 2
  • ‘%’ for modulo, Example: var e = 100 % 50

Including Javascript to a page

When you want to add Javascript to a web page, you use the script <> tags.

Syntax example:

<script type="text/javascript">
//here you write the Javascript code
</script>

Writing comments

Comments, though not strictly part of the code, help break down your programming code into easily decipherable text that helps you or others who will be seeing your code to understand what a particular snippet stands for or what is going behind the scenes. In a sense, they are notes for your program so that you can remember why a certain code is in the way it is.

However, you can not simply type comments along with a code statement just as you would do while typing some text. Instead, you need to properly assign comments so that they are not executed when you run your program. The two types of comments are:

Single line comments: These are marked as ‘comment’ with double slashes ( // ) and should only be used when you want to write a small, precise comment.

Long comments: If your comment is of multiple lines and you do not want to execute it, you need to wrap it with /* and */ to mark it as a comment.

Accessing libraries

A library in Javascript has pre-created methods that can instantly be called upon to carry some function.

Syntax: To call a library, first write the library name, follow it with a period ( . ) and then with the method, and finally the parenthesis.

For example:

Math.random();

Here, Math is the library with methods like ‘random’ in it.

Examples: Math.random()

The Math.random() function, when called, executes to print a random floating-point from 0 to 0.9.

Example:

console.log(Math.random());
// Prints: 0 - 0.9

Math.floor()

The Match.floor() function is used to print the largest integer, which is less than or equal to the number provided.

Example:

console.log(Math.floor(7.95));
// Prints: 7

Naming variables in JS

Variables can be understood as non-fixed values, which can be assigned values at the time of carrying operations or executing code in case of var or as in stand-in values for other variables.

These are of three types:

var – The var variable can be reassigned and is only accessible from within a function.

const – These variables can not be reassigned and are only accessible when they appear in the code.

let – The variable can be reassigned values but can only be declared once.

Arrays

Arrays in any programming language are used as a way to sort out variables and properties into groups or categories. Hence, one specific group can have many variables which you can use for further programming.

Example: An array named ‘planet’ can have many variables like ‘earth,’ ‘Jupiter, ‘mars,’ ‘mercury,’ and more.

var planet = ["Earth", "Mars", "Jupiter", "Mercury"];

Array methods

Arrays can be joined, combined, spliced, shifted, and more using array methods. Here are a few most used array methods in Javascript:

  • concat() — contact() is a method that helps join several arrays into one.
  • Join () — join() method helps combine the elements of an array into a single string. It also returns the string.
  • indexOf() — the indexOf() method returns with the value of the first position of the element entered.
  • lastIndexOf() — this method is used to return the last position of an element in an array
  • pop() — the pop() helps remove the last element of an array.
  • shift() — you can remove the first element of an array using shift().
  • push() — a new element can be added to the end of an array using the push() method.
  • reverse() — elements can be sorted in descending order using this method.
  • sort() — elements can be sorted alphabetically using sort().
  • slice() — the slice() method creates a new array with a copy of the selected portion of an array.
  • toString() — elements in an array can be converted into a string using toString().
  • unshift() — Opposite to shift(), unshift() adds an element to the beginning of an array.

This basic cheat sheet for beginner programmers can help them get the hang of Javascript programming and get the most out of it.

   

About the Author

ByteScout Team ByteScout Team of Writers ByteScout has a team of professional writers proficient in different technical topics. We select the best writers to cover interesting and trending topics for our readers. We love developers and we hope our articles help you learn about programming and programmers.  
prev
next