DEV Community

Aliyu Adeniji
Aliyu Adeniji

Posted on • Updated on

Introduction to JavaScript

JavaScript Variables

Variables are data containers which stores values and information we want the computer to remember in subsequent times. Variables can be changed by simply changing the values of an existing variable to a new one.
Variables are the memory of JavaScript, lets create some variables
var myName="Aliyu" this will save into JavaScript's memory as my name, so next time I try to recall the variable in a function, it'll bring Aliyu as its value.

JavaScript Strings

Stings in JavaScript are values that are enclosed in quotation marks, there are a lot of operations you can do with strings which include printing a value which can be a combination of multiple strings. One of the things you can do with strings is calculating the strings' length and retrieving the number of characters in a string. This can be with in the following syntax;
To count the number of characters in the variable above simply try: myName.length; and teh result will be 5.
Try some code to allow javascript to tell you the number of characters you have written and the number remaining to enter

var tweet = prompt("what is your name");
var tweetcount = tweet.length;
alert("You have written" + tweetcount + "characters, you have" +(200 - tweetcount) + "characters remaining");

Slicing and extracting part of a string

var tweet = prompt("what is your name");
var tweetunder50 = tweet.slice(0,50);
alert(tweetunder50);

you can also shorten your syntax by putting the functions on a single line
alert(prompt("what is your name").slice(0,50));

Changing the characters in a string.

// var name = prompt("What is your name");
// name = name.toUpperCase();
// alert("hello" + " " + name.toUpperCase());
var firstChar = name.slice(0,1);
var remainingchar = name.slice(1,name.length);
// alert(remainingchar);
var lowercaseremainingchar = remainingchar.toLowerCase();
alert(lowercaseremainingchar);

Top comments (0)